データベース構築
先日インストールしたPostgreSQL-8.3のデータベースを構築する。構築する項目は下記の通り。
- インスタンス
- ユーザ/パスワード
- データベース
データ保存ディレクトリの作成
まずは何よりも最初に必要なインスタンスの構築から。
自分は『インスタンス』と呼んでしまっているのだが、世間一般的・技術的(?)には何て言うのかは正直知らない。PostgreSQLは複数のデータベースインスタンスを1台のサーバで起動する事が可能である。まぁ何よりまずはインスタンス構築まで。
構築は全てPostgreSQLを起動するユーザにて行う。これ必須。
PostgreSQLのデータを保存するディレクトリを作成する。
この検証環境はデータは全て/opt配下に配置する事にした。まずはディレクトリ作成。
[postgres@CentOS-5.2 ~]$ su - パスワード: ********** [root@CentOS-5.2 ~]# cd /opt [root@CentOS-5.2 opt]# ls -l 合計 0 [root@CentOS-5.2 opt]# mkdir postgresql-8.3.3 [root@CentOS-5.2 opt]# ls -ld postgresql-8.3.3/ drwxr-xr-x 2 root root 4096 8月 8 10:11 postgresql-8.3.3/ [root@CentOS-5.2 opt]# chown postgres:postgres postgresql-8.3.3/ [root@CentOS-5.2 opt]# ls -ld postgresql-8.3.3/ drwxr-xr-x 2 postgres postgres 4096 8月 8 10:11 postgresql-8.3.3/ [root@CentOS-5.2 opt]#
インスタンス構築(initdb)
実際のデータベースインスタンス構築。
initdbコマンドのオプションは--helpで確認する。
今回は--no-localeと--encoding、データの保存先である-Dオプションのみを指定する事にする。
ちなみにinitdbコマンドへのパスは通っていないので絶対パスで指定する。
[postgres@CentOS-5.2 postgresql-8.3.3]$ /usr/local/postgresql-8.3.3/bin/initdb --no-locale --encoding=UTF8 -D /opt/postgresql-8.3.3 The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale C. The default database encoding has accordingly been set to SQL_ASCII. The default text search configuration will be set to "english". fixing permissions on existing directory /opt/postgresql-8.3.3 ... ok creating subdirectories ... ok selecting default max_connections ... 100 selecting default shared_buffers/max_fsm_pages ... 32MB/204800 creating configuration files ... ok creating template1 database in /opt/postgresql-8.3.3/base/1 ... ok initializing pg_authid ... ok initializing dependencies ... ok creating system views ... ok loading system objects' descriptions ... ok creating conversions ... ok creating dictionaries ... ok setting privileges on built-in objects ... ok creating information schema ... 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 -A option the next time you run initdb. Success. You can now start the database server using: /usr/local/postgresql-8.3.3/bin/postgres -D /opt/postgresql-8.3.3 or /usr/local/postgresql-8.3.3/bin/pg_ctl -D /opt/postgresql-8.3.3 -l logfile start [postgres@CentOS-5.2 postgresql-8.3.3]$
ディレクトリやファイルができているか確認。
[postgres@CentOS-5.2 postgresql-8.3.3]$ ls -l 合計 64 -rw------- 1 postgres postgres 4 8月 8 10:16 PG_VERSION drwx------ 5 postgres postgres 4096 8月 8 10:16 base drwx------ 2 postgres postgres 4096 8月 8 10:16 global drwx------ 2 postgres postgres 4096 8月 8 10:16 pg_clog -rw------- 1 postgres postgres 3429 8月 8 10:16 pg_hba.conf -rw------- 1 postgres postgres 1460 8月 8 10:16 pg_ident.conf drwx------ 4 postgres postgres 4096 8月 8 10:16 pg_multixact drwx------ 2 postgres postgres 4096 8月 8 10:16 pg_subtrans drwx------ 2 postgres postgres 4096 8月 8 10:16 pg_tblspc drwx------ 2 postgres postgres 4096 8月 8 10:16 pg_twophase drwx------ 3 postgres postgres 4096 8月 8 10:16 pg_xlog -rw------- 1 postgres postgres 16552 8月 8 10:16 postgresql.conf [postgres@CentOS-5.2 postgresql-8.3.3]$
インスタンスが構築できた。
ちなみにinitdbコマンドの-Dオプションで指定したディレクトリは・・・
[postgres@curious-52 ~]$ ls -ld /opt/postgresql-8.3.3/ drwx------ 10 postgres postgres 4096 8月 8 10:39 /opt/postgresql-8.3.3/ [postgres@curious-52 ~]$
パーミッションが700になってinitdbを実行したOSユーザ以外のアクセスを制限されている。
インスタンス起動
initdbコマンドを実行した時に表示されていたように起動する。
[postgres@curious-52 ~]$ /usr/local/postgresql-8.3.3/bin/pg_ctl start -D /opt/postgresql-8.3.3 server starting [postgres@curious-52 ~]$ LOG: database system was shut down at 2008-08-08 10:16:42 JST LOG: autovacuum launcher started LOG: database system is ready to accept connections [postgres@curious-52 ~]$
上記コマンドではinitdbコマンド実行時の表示から-lオプションを抜いている。この場合ログ出力がされずに標準出力・標準エラー出力となる。
プロセスでPostgreSQLが動作しているかを確認する。
[postgres@curious-52 ~]$ ps -ef | grep postgres root 2401 2272 0 10:09 pts/0 00:00:00 su - postgres postgres 2402 2401 0 10:09 pts/0 00:00:00 -bash postgres 2633 1 0 10:22 pts/0 00:00:00 /usr/local/postgresql-8.3.3/bin/postgres -D /opt/postgresql-8.3.3 postgres 2635 2633 0 10:22 ? 00:00:00 postgres: writer process postgres 2636 2633 0 10:22 ? 00:00:00 postgres: wal writer process postgres 2637 2633 0 10:22 ? 00:00:00 postgres: autovacuum launcher process postgres 2638 2633 0 10:22 ? 00:00:00 postgres: stats collector process postgres 2649 2402 0 10:25 pts/0 00:00:00 ps -ef postgres 2650 2402 0 10:25 pts/0 00:00:00 grep postgres [postgres@curious-52 ~]$
データベースへ接続
まずはデータベースインスタンスへ接続する。クライアントアプリケーションとしてpsqlコマンド。ポート等は設定を変えていないのでデフォルトまま。
[postgres@curious-52 ~]$ /usr/local/postgresql-8.3.3/bin/psql postgres Welcome to psql 8.3.3, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quit postgres=#
上記の場合、デフォルト設定がまま使用されているので実際には下記のコマンドを実行しているものと同じ状態。もちろん下記コマンドでも接続可能なはず。
[postgres@curious-52 ~]$ /usr/local/postgresql-8.3.3/bin/psql -p 5432 -U postgres postgres Welcome to psql 8.3.3, the PostgreSQL interactive terminal. Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quit postgres=#
- Uオプションに自動に入るユーザはコマンドを実行しているOSユーザ。最後に記述しているpostgresはユーザではなく接続対象データベース名。PostgreSQLではデフォルトでpostgres, template0, template1の3つのデータベースが作成されている。それぞれ…それは別のエントリで。接続データベースを指定しない場合はコマンドを実行するOSユーザ名と同名のデータベースに接続しようとするらしい。
ユーザ作成
ユーザの作成にはcreateuserコマンドを実行する方法と、psqlで接続語にSQLで作成する方法がある。
せっかくだから両方試してみよう!
- createuserコマンド
オプションもあるのだが、引数を何も渡さないと対話式で設定できるので今回は対話式で実行する。
[postgres@curious-52 ~]$ /usr/local/postgresql-8.3.3/bin/createuser Enter name of role to add: dbuser1 Shall the new role be a superuser? (y/n) n Shall the new role be allowed to create databases? (y/n) n Shall the new role be allowed to create more new roles? (y/n) n [postgres@curious-52 ~]$
- SQL(create user)
オプションは何も指定しないとデフォルトで作成される。
postgres=# create user dbuser2; CREATE ROLE postgres=#
- SQL(create role)
create userはもう古い!create roleを実行しろ!というマニュアルか何かを見た事がある記憶があったので実行してみた。こちらもオプションは何も指定しないで作成してみる。
postgres=# create role dbuser3; CREATE ROLE postgres=#
3つの方法にて作成されたユーザの一覧をpsqlで確認する。
postgres=# \du List of roles Role name | Superuser | Create role | Create DB | Connections | Member of -----------+-----------+-------------+-----------+-------------+----------- dbuser1 | no | no | no | no limit | {} dbuser2 | no | no | no | no limit | {} dbuser3 | no | no | no | no limit | {} postgres | yes | yes | yes | no limit | {} (4 rows) postgres=#
どれも同じようにユーザ(role)が作成されている事がわかる。
データベース作成
これまたcreatedbコマンドを実行する方法とSQLで実行する方法とがあるので両方試してみる。
- createdbコマンド
createdbコマンドはcreateuserコマンドとは違って対話式にはいかないよだ。オプションは--helpで確認しましょう。
今回は--owner(-O)オプションのみを指定する事にしよう。
[postgres@curious-52 ~]$ /usr/local/postgresql-8.3.3/bin/createdb --owner=dbuser1 testdb1 [postgres@curious-52 ~]$
- SQL(create database)
SQLでの実行。
postgres=# create database testdb2 with owner dbuser2; CREATE DATABASE postgres=#
データベースが作成されているか確認。
postgres=# \l List of databases Name | Owner | Encoding -----------+----------+---------- postgres | postgres | UTF8 template0 | postgres | UTF8 template1 | postgres | UTF8 testdb1 | dbuser1 | UTF8 testdb2 | dbuser2 | UTF8 (5 rows) postgres=#
構築されたようだ。
インスタンス停止
停止の方法。
[postgres@curious-52 ~]$ /usr/local/postgresql-8.3.3/bin/pg_ctl stop -D /opt/postgresql-8.3.3/ LOG: received smart shutdown request LOG: autovacuum launcher shutting down LOG: shutting down waiting for server to shut down....LOG: database system is shut down done server stopped [postgres@curious-52 ~]$
上記オプションではPostgreSQLのプロセスが落ちない場合がある。誰かが接続していたままだったりだと落ちないようだ。その時にはオプションで「-m fast」「-m immediate」といったオプションがあるけれども詳細はまたどこかのエントリで。
PostgreSQLが停止している事をプロセスでも確認しておこう。
[postgres@curious-52 ~]$ ps -ef | grep postgres root 2401 2272 0 10:09 pts/0 00:00:00 su - postgres postgres 2402 2401 0 10:09 pts/0 00:00:00 -bash postgres 2713 2402 0 10:35 pts/0 00:00:00 ps -ef postgres 2714 2402 0 10:35 pts/0 00:00:00 grep postgres [postgres@curious-52 ~]$
あやしいプロセスはいないね。
残タスク
データベースを正常に使用する為に必要な残タスクは下記の通り。
- postgresql.conf
- pg_hba.conf
最低限これらの設定も入れてあげないとね。
特にpg_hba.confはこのエントリで設定したユーザのパスワードを有効化する為には絶対に必要。
今日は長くなったのでここまで♪
コマンド/ファイル
- コマンド
- /bin/mkdir
- PostgreSQLコマンド
- createdb
- createuser
- initdb
- pg_ctl
- psql
- psql内部コマンド
- \du
- \l