MacPorts で PostgreSQL インストール後にする最初の設定

インストール直後は、PostgreSQL にパスワードなしで接続できるため、まずは管理者のパスワードを設定する。
この設定は、PostgreSQL が起動していないと設定ができないので、まずは PostgreSQL を起動する。

% sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql84/defaultdb -l /opt/local/var/db/postgresql84/logfile start'
server starting

起動後に、psql (postmaster) で PostgreSQL に接続する。

% psql84 -U postgres
psql84 (8.4.3)
Type "help" for help.

postgres=# 

接続後、登録されているユーザーを確認する。

postgres=# \du
            List of roles
 Role name | Attributes  | Member of 
-----------+-------------+-----------
 postgres  | Superuser   | {}
           : Create role   
           : Create DB  

そして、パスワードの設定をし、psql を終了する。

postgres=# ALTER USER postgres WITH PASSWORD 'yourpassword';
ALTER ROLE
postgres=# \q

次に、PostgreSQL のユーザー認証設定ファイルを変更する。

% sudo vi /opt/local/var/db/postgresql84/defaultdb/pg_hba.conf
Password:

変更前の内容は下記の通り。

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
# "local" is for Unix domain socket connections only
local   all         all                               trust
# IPv4 local connections:
host    all         all         127.0.0.1/32          trust
# IPv6 local connections:
host    all         all         ::1/128               trust

METHOD に割り当てられている「trust」を「passowrd」に変更する。変更後は下記の通り。

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD
# "local" is for Unix domain socket connections only
local   all         all                               password
# IPv4 local connections:
host    all         all         127.0.0.1/32          password
# IPv6 local connections:
host    all         all         ::1/128               password

設定ファイルを保存したら、変更内容を反映させる。

% sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql84/defaultdb -l /opt/local/var/db/postgresql84/logfile reload'

もしくは

% sudo su postgres -c 'pg_ctl -D /opt/local/var/db/postgresql84/defaultdb -l /opt/local/var/db/postgresql84/logfile restart'

で変更内容が反映される。そして、パスワードの入力を求められるか確認する。

% psql84 -U postgres
Password for user postgres: 
psql84 (8.4.3)
Type "help" for help.

postgres=#