PostgreSQL 8.4のpg_hba.confの設定変更を反映するには、サービスのリロードでOK

Linux(CentOS 6.6)にインストールしているPostgreSQL 8.4を使用しようと思って、Tera Termでpsqlコマンドを実行すると、接続できなかった。

[root@test ~]# psql -U postgres
psql: FATAL: ユーザ"postgres"のIdent認証に失敗しました

PostgreSQLの接続制限をident認証で行っている場合、Linux操作ユーザとPostgreSQLのデータベースユーザが異なると、psql実行時に、-Uオプションでユーザをpostgresと指定していてもident認証には失敗することを思い出した。

Linuxの操作ユーザをpostgresユーザに変更してpsqlを実行してみる。

[root@test tmp]# su - postgres
-bash-4.1$ psql
psql (8.4.20)
"help" でヘルプを表示します.

postgres=#

Linuxの操作ユーザをpostgresに変更してからpsqlを実行するとデータベース接続に成功することが分かった。

このまま使用してもよかったとは思うが、私以外使わない自宅の実験的なLinuxサーバで気軽に使うには面倒かなぁと思い、pg_hba.confの設定を変えることにした。

[root@test ~]# cd /var/lib/pgsql/
[root@test pgsql]# ls -ahl
合計 32K
drwx------. 4 postgres postgres 4.0K 1月 28 08:52 2015 .
drwxr-xr-x. 18 root root 4.0K 5月 6 15:18 2013 ..
-rw-------. 1 postgres postgres 10 9月 5 01:24 2014 .bash_history
-rw-r--r--. 1 postgres postgres 85 2月 26 02:13 2014 .bash_profile
-rw-------. 1 postgres postgres 143 9月 5 01:24 2014 .psql_history
drwx------. 2 postgres postgres 4.0K 2月 26 02:13 2014 backups
drwx------. 12 postgres postgres 4.0K 1月 28 08:54 2015 data
-rw-------. 1 postgres postgres 1.8K 5月 6 15:30 2013 pgstartup.log
[root@test pgsql]# cd data/
[root@test data]# ls -ahl
合計 88K
drwx------. 12 postgres postgres 4.0K 1月 28 08:54 2015 .
drwx------. 4 postgres postgres 4.0K 1月 28 08:52 2015 ..
-rw-------. 1 postgres postgres 4 5月 6 15:30 2013 PG_VERSION
drwx------. 5 postgres postgres 4.0K 5月 6 15:30 2013 base
drwx------. 2 postgres postgres 4.0K 1月 28 08:54 2015 global
drwx------. 2 postgres postgres 4.0K 5月 6 15:30 2013 pg_clog
-rw-------. 1 postgres postgres 3.4K 5月 6 15:30 2013 pg_hba.conf
-rw-------. 1 postgres postgres 1.6K 5月 6 15:30 2013 pg_ident.conf
drwx------. 2 postgres postgres 4.0K 1月 8 00:00 2015 pg_log
drwx------. 4 postgres postgres 4.0K 5月 6 15:30 2013 pg_multixact
drwx------. 2 postgres postgres 4.0K 2月 7 13:50 2015 pg_stat_tmp
drwx------. 2 postgres postgres 4.0K 5月 6 15:30 2013 pg_subtrans
drwx------. 2 postgres postgres 4.0K 5月 6 15:30 2013 pg_tblspc
drwx------. 2 postgres postgres 4.0K 5月 6 15:30 2013 pg_twophase
drwx------. 3 postgres postgres 4.0K 5月 6 15:30 2013 pg_xlog
-rw-------. 1 postgres postgres 17K 5月 6 15:30 2013 postgresql.conf
-rw-------. 1 postgres postgres 57 1月 28 08:54 2015 postmaster.opts
-rw-------. 1 postgres postgres 45 1月 28 08:54 2015 postmaster.pid
[root@test data]# vi pg_hba.conf   ←編集前にバックアップを行うこと。
# PostgreSQL Client Authentication Configuration File
# ===================================================
#
# Refer to the "Client Authentication" section in the
# PostgreSQL documentation for a complete description
# of this file. A short synopsis follows.
#
# This file controls: which hosts are allowed to connect, how clients
# are authenticated, which PostgreSQL user names they can use, which
# databases they can access. Records take one of these forms:
#
# local DATABASE USER METHOD [OPTIONS]
# host DATABASE USER CIDR-ADDRESS METHOD [OPTIONS]
# hostssl DATABASE USER CIDR-ADDRESS METHOD [OPTIONS]
# hostnossl DATABASE USER CIDR-ADDRESS METHOD [OPTIONS]
#
# (The uppercase items must be replaced by actual values.)
#
# The first field is the connection type: "local" is a Unix-domain socket,
# "host" is either a plain or SSL-encrypted TCP/IP socket, "hostssl" is an
# SSL-encrypted TCP/IP socket, and "hostnossl" is a plain TCP/IP socket.
#
# DATABASE can be "all", "sameuser", "samerole", a database name, or
# a comma-separated list thereof.
#
# USER can be "all", a user name, a group name prefixed with "+", or
# a comma-separated list thereof. In both the DATABASE and USER fields
# you can also write a file name prefixed with "@" to include names from
# a separate file.
#
# CIDR-ADDRESS specifies the set of hosts the record matches.
# It is made up of an IP address and a CIDR mask that is an integer
# (between 0 and 32 (IPv4) or 128 (IPv6) inclusive) that specifies
# the number of significant bits in the mask. Alternatively, you can write
# an IP address and netmask in separate columns to specify the set of hosts.
#
# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi", "krb5",
# "ident", "pam", "ldap" or "cert". Note that "password" sends passwords
# in clear text; "md5" is preferred since it sends encrypted passwords.
#
# OPTIONS are a set of options for the authentication in the format
# NAME=VALUE. The available options depend on the different authentication
# methods - refer to the "Client Authentication" section in the documentation
# for a list of which options are available for which authentication methods.
#
# Database and user names containing spaces, commas, quotes and other special
# characters must be quoted. Quoting one of the keywords "all", "sameuser" or
# "samerole" makes the name lose its special character, and just match a
# database or username with that name.
#
# This file is read on server startup and when the postmaster receives
# a SIGHUP signal. If you edit the file on a running system, you have
# to SIGHUP the postmaster for the changes to take effect. You can use
# "pg_ctl reload" to do that.

# Put your actual configuration here
# ----------------------------------
#
# If you want to allow non-local connections, you need to add more
# "host" records. In that case you will also need to make PostgreSQL listen
# on a non-local interface via the listen_addresses configuration parameter,
# or via the -i or -h command line switches.
#

# TYPE DATABASE USER CIDR-ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all ident
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident

どうやらpg_hba.confは初期設定(インストール直後)の状態のままだったようだ。

Linuxコンソール上でちょっと使ってみるだけだったから、localの設定をidentからtrustに変更した。(セキュリティを考慮するのであれば、trustの部分はmd5などにすること)

《変更前》
# "local" is for Unix domain socket connections only
local all all ident


《変更後》
# "local" is for Unix domain socket connections only
local all all trust

さて、pg_hba.confの設定変更を反映させるには、PostgreSQLの再起動、リロードのどちらで行うのか、について少し気になった。

PostgreSQLの再起動を行えば反映されるのは間違いないだろうが、リロードでも反映されるのだろうか。

試しに、PostgreSQLサービスのリロードコマンドを実行してみる。

[root@test data] /etc/rc.d/init.d/postgresql reload

PostgreSQLサービスのリロードだけで、pg_hba.confの設定変更については、反映されることを確認した。

といった実験だけで断定するのはよくないかな、と思い、PostgreSQL 8.4のマニュアルを確認すると、以下の記述があった。

《参考》PostgreSQL 8.4.4文書  第 19章クライアント認証  19.1. pg_hba.confファイル
http://www.postgresql.jp/document/8.4/html/auth-pg-hba-conf.html

pg_hba.confファイルは起動時と、主サーバプロセスがSIGHUP信号を受け取った時に読み込まれます。 稼働中のシステムでファイルを編集した場合は、(pg_ctl reloadあるいはkill -HUPを使用して)サーバにファイルをもう一度読み込むように信号を出さなければなりません。

PostgreSQL 8.4のマニュアルにも書いてあることだし、実際に試してみて確認した結果、PostgreSQL 8.4のpg_hba.confの設定変更を反映するには、PostgreSQLサービスのリロードのみでよいことが分かった。

前へ

「セルフ 讃岐うどん 架け橋 夢 うどん」(瀬戸大橋与島パーキングエリア 与島プラザ)でうどんを食べた

次へ

LinuxのcurlコマンドでWebページのヘッダ情報を確認する方法