/dev/null

(◞‸◟)

CentOS 6.4にインストールしたPostgreSQL9.2.4を起動させてみる

はい、昨日にインストールしたPostgreSQLを起動させてみよう。

これ書き始めた時点で起動出来てません、不安ですね。

何はともあれ,initdbをやってみる。ちなみに/usr/local/pgsql/binにパスが通ってる前提です。

[~]$ initdb

initdb: no data directory specified

You must identify the directory where the data for this database system

will reside.  Do this with either the invocation option -D or the

environment variable PGDATA.

うーん。

データディレクトリを指定してねっと事かしら。

-Dオプションで指定かPGDATAっていう環境変数をexportしておく必要がありそう。

とりあえず-Dオプションで動かすのも良さそうですが毎回やるのも忘れそうなので~/.zshrcか~/.bashrcに書きましょう。

使っているシェルに合わせて

vi ~/.bashrc

vi ~/.zshrc

↓を追記

export PGDATA=/usr/local/pgsql/data

反映

source .bashrc

source .zshrc

実行してみる

[~]$ sudo initdb                                                                   
sudo: initdb: コマンドが見つかりません

ファッ!?

なんか、sudo実行時は自分で追加したPATHのディレクトリは呼ばれない模様。

仕方がないのでsudo なし

[~]$ initdb                                                                                                                [18:23:32]
The files belonging to this database system will be owned by user "user".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

fixing permissions on existing directory /usr/local/pgsql/data ... initdb: could not change permissions of directory "/usr/local/pgsql/data": 許可さ れていない操作です

/usr/local/pgsql/data の所有権はお前にないってことだ。

よろしいならばsudo付きフルパスだ

[~]$ sudo /usr/local/pgsql/bin/initdb

initdb: no data directory specified
You must identify the directory where the data for this database system
will reside.  Do this with either the invocation option -D or the
environment variable PGDATA.

ファ------wwwwwwwwwwwww

sudoでは自分で設定した環境変数は全部持ってこれないからPGDATAも駄目なんすねwww

そろそろ辛まってきた。

じゃあもう-Dオプションでとりあえずなんとかならんか

[~]$ sudo /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data/

initdb: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

(•́ω•̀)........

要するに、sudoとかrootとかじゃpostgresqlは動かせない。

そのためにユーザを作って(普通はpostgresユーザー)、そいつに各種ディレクトリを所有させて動かせってことかな????????

だりーな????

いまはuserという名前のユーザーで動かしてるから、/usr/local/pgsql/dataをこいつの所有権で動かせばおk???

sudo chown user:user /usr/local/pgsql/data/

これでディレクトリ所有権がこいつに。

ちなみに忘れてたけどsudo mkdir /usr/local/pgsql/dataしておけよ

で、何度目だinitdb

[~]$ initdb   

The files belonging to this database system will be owned by user "user".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

fixing permissions on existing directory /usr/local/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /usr/local/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    postgres -D /usr/local/pgsql/data
or
    pg_ctl -D /usr/local/pgsql/data -l logfile start

なんか成功したっぽいですね。

次はdbを起動させてみるんです。

[~]$  pg_ctl -D /usr/local/pgsql/data -l logfile start                                                                     
server starting

動きだしたっぽい

で、次は接続。いつもみんながやるやつね。

psql postgres user

これはpostgresっていう元からあるデータベースにuserという名前のユーザーで接続するっていう意味。

userはinitdbしたユーザーなのである意味最強。なんでも出来ちゃうrootみたいなもの。

これでは良くないので普通のユーザーさんを作ります。

コマンドはcreateuser。ちな、postgreSQLがstartされてる必要がある模様

[~]$ which createuser                     
/usr/local/pgsql/bin/createuser


[~]$ createuser postgres  

作れました。

このユーザーで接続してみる

[~]$ psql postgres postgres                                                                                                [18:44:25]
psql (9.2.4)
Type "help" for help.

postgres=> 

おkおk.

postgresデータベースじゃなくて自分のDBを作りましょう。

createdb -E utf-8 -O postgres -U postgres testdb

これはそれぞれ、utf-8エンコーディング変換で、オーナーはpostgresさん、ユーザーもpostgresさんという指定

オーナーというのがDBの所有者、ユーザーは接続するユーザーです。

[~]$ createdb -E utf-8 -O postgres -U postgres 

createdb: database creation failed: ERROR:  permission denied to create database

oh...........createuserするときにDBを作る権限を上げていないのが問題なようです。

一旦削除

dropuser postgres

もっかい作る。オプション付き

createuser -a -d -U user postgres

-aでこのユーザーが新しいユーザを作ることを許可

-dでデータベース作ることを許可

-UはPostgreSQLに接続するユーザ名

あんまり-Uわかってない。

なにはともあれDB作ろう。セキュリティ的にはuserだけがDB作れて、userが作ったテーブルを他ユーザーが接続する形が良いのかな。

[~]$ createdb -E utf-8 -O postgres -U postgres testdb

特にエラーが出なければ完了。

接続してみよう。

psql testdb postgres   
psql (9.2.4)
Type "help" for help.

testdb=# 

出来ました✌('ω')✌

あとは適宜ほしいテーブルを作ったり

testdb=# create table test(id int,name varchar(10));
CREATE TABLE

データ追加したり

testdb=# insert into test values (1,'hellowork');
INSERT 0 1

ちなみにpostgreSQLを止めるには

pg_ctl stop

です。

結構時間かかったけどこんな感じかな。Master/SLAVE構築はまた今度。

次はMySQLかな。