PostgreSQL 8.3.5の設定メモ

PostgreSQLの外部マシンからSSL接続するための設定の確認をした。
環境はデスクトップ機にUbuntuをインストール。

  • Ubunto server 8.04.2 LTS
  • Lenovo ThinkCenter A61(9120A21)
  • AMD Athlon64 X2 4000+
  • RAM 2GB

8.04.2 LTSをインストールする際にオプションで選んでおけば、postgresとopenSSLはインストールされていた。
デフォルトは外部からpostgresにアクセスできない状態。

設定ファイルは以下の2つ


ローカルから接続してみる。
UNIXソケットの接続は接続できる。

$ psql template1

TCP/IPソケットはエラーで接続できない。

$ psql -h 127.0.0.1 template1

postgresql.confを見ると、

ssl = true

になっているので、デフォルトでSSL接続になるが、ほかの行がコメントアウトしているので、そのままではできない。
また、pg_hda.confをみると、デフォルトはTCP/IPソケットはmd5で認証されるようになっていた。


確認で手間取ったのは、Linuxに登録されたユーザーとpostgresに登録されるユーザーが違う点。
どちらにもpostgresという名前のユーザーがいる(のかな)。
たぶんパスワードの初期値がnullになっている。
結果として、データベースユーザーpostgresにパスワードを設定しないと、エラーでSSL接続できない。

ユーザーをpostgresに切り替える。

$ su postgres

データベースユーザーpostgresのパスワードを設定する。

$ psql -U postgres template1
template1=# ALTER USER postgres WITH PASSWORD 'newpassword';

postgresql.confの設定変更をする。

つぎの行をコメントアウトを外す。

# listen_addresses = 'localhost'

ローカルから接続してみる。

$ psql -h 127.0.0.1 template1
Password:
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.

Type: \copyright for distribution terms
\h for help with SQL commands
\? for help on internal slash commands
\g or terminate with semicolon to execute query
\q to quit

SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256)

最後のSSL connection・・が、tcpipでSSL接続ができた状態.



外部から待ち受けるためにlocalhostを*に変更。

listen_addresses = '*'


pg_hda.confの設定変更をする。
以下はデフォルトの内容。

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

外部から接続するために、1行追加する。
webサーバが決まったら、そのアドレスでいいんだろうけど、とりあえずローカルエリアを加える。

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


外部からのアクセスは、「pgAdmin III」というGUIを使ってみた。
設定不備の場合、どの設定がどうなってるよ、と警告してくれる。
postgresql.confにあるが、接続ポートは'5432'
接続してみると、プロパティでSSL暗号化で接続しているのが確認できる。

  • ポート:5432
  • ユーザー名:postgres
  • パスワード:newpassword