ユーザにパスワードを設定する

PostgreSQLのスーパーユーザであるpostgresに対してパスワードを設定する事にする。ネットワーク経由(TCP/IP)からの接続の場合において、設定したパスワードが一致しなければ接続を制限するようにしたい。
まずは検証サーバにて確認。

ユーザの一覧を確認

念の為、現在どんなユーザが検証サーバのPostgreSQLに存在するかを確認する。現状ではPostgreSQLのプロセスを起動しているユーザであれば、localhostからの接続にはパスワードの認証も何もない。

[postgres@CentOS-5.2 ~]$ /usr/local/postgresql-8.3.3/bin/psql 
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=# \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=# 

パスワード変更SQL

下記SQLによってpostgresユーザに対してパスワードを設定する。

postgres=# alter role postgres with password 'postgres';
ALTER ROLE
postgres=# 

上記例はpostgresユーザに対して『postgres』というパスワードを設定したところ。
pg_hba.confの設定と合わせて動作を確認しよう。