[PostgreSQL] Configuration for Connecting from Other PCs

Tadashi Shigeoka ·  Sun, March 11, 2012

I looked up the configuration for connecting from other hosts to PostgreSQL, so taking notes.

postgresq.confファイルの修正 / Modifying postgresql.conf file

■ /usr/local/pgsql/data/postgresql.conf

#listen_addresses = 'localhost' # what IP interface(s) to listen on; 
                                # defaults to localhost, '*' = any

↓(Change to)

listen_addresses = '*'

After changing the configuration, restart PostgreSQL.

/etc/rc.d/init.d/postgresql restart

接続できるクライアントを設定する / Configure Connectable Clients

PostgreSQLの設定ファイルである pg_hba.conf にて、接続できるホストを制限しているので、これを変更します。 「The PostgreSQL configuration file pg_hba.conf restricts connectable hosts, so we need to change this.」

■ /usr/local/pgsql/data/pg_hba.conf

# 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

■(例)全てのホストからの接続を許可する / Example: Allow connections from all hosts

全てのホストから、全てのデータベースへの接続を許可する場合、以下の記述を追加する。 「To allow connections from all hosts to all databases, add the following line:」

host all all 0.0.0.0/0 trust

接続を制限する / Restricting Connections

pg_hba.confは1行で1レコードを構成しており、1行のフォーマットは以下のとおりである。 「pg_hba.conf consists of one record per line, with the following format for each line:」

KIND DATABASE USER CIDR-ADDRESS METHOD

● KIND

「local」「host」「hostssl」「hostnossl」のいずれか。localはUNIXドメインソケット経由の接続、hostはTCP/IP経由の接続、hostsslはSSL経由の接続、hostnosslはSSLを使用しない接続を意味する。 「One of ‘local’, ‘host’, ‘hostssl’, or ‘hostnossl’. Local means connections via UNIX domain sockets, host means connections via TCP/IP, hostssl means connections via SSL, and hostnossl means connections without SSL.」

● DATABASE 接続を許可したいデータベース名を指定。「all」とすると全てのデータベースを意味する。複数のデータベース名を指定したい場合は、カンマで区切る。 「Specify the database name to allow connections to. ‘all’ means all databases. To specify multiple database names, separate them with commas.」

● USER 接続を許可するユーザ名を指定。「all」とすると全てのユーザを意味する。ユーザ名の前に「+」を付けるとグループ名を指定したことになる。複数のユーザ名を指定したい場合は、カンマで区切る。 「Specify the username to allow connections for. ‘all’ means all users. Adding ’+’ before a username specifies a group name. To specify multiple usernames, separate them with commas.」

● CIDR-ADDRESS KINDがlocal以外のときに指定する。接続を許可するクライアントのIPアドレスやネットワークアドレスを指定する。 「Specify when KIND is other than ‘local’. Specify the IP address or network address of clients to allow connections from.」

例1)192.168.0.11/32・・・192.168.0.11からの接続のみを許可する 例2)192.168.0.0/24・・・IPアドレスが192.168.0.xであるクライアントのみを許可する 例3)0.0.0.0/0・・・任意のIPアドレスのクライアントを許可する 「Example 1) 192.168.0.11/32 - Allow connections only from 192.168.0.11 Example 2) 192.168.0.0/24 - Allow only clients with IP addresses 192.168.0.x
Example 3) 0.0.0.0/0 - Allow clients with any IP address」

● METHOD ユーザの認証方式を指定する。代表的なものは以下のとおり。 trust・・・認証なし。無条件に接続を許可する。 reject・・・無条件に接続を拒否する。特定のホストやネットワークからの接続を拒否する際に使用。 md5・・・md5を利用したパスワード認証。パスワードを指定していないユーザは接続できない。 password・・・パスワード認証を行うが、BASIC認証のため、パスワードがそのままネットワークを流れてしまう。 「Specify user authentication method. Common ones include: trust - No authentication. Allows connections unconditionally. reject - Unconditionally reject connections. Used to reject connections from specific hosts or networks. md5 - Password authentication using md5. Users without passwords cannot connect. password - Password authentication, but uses BASIC authentication so passwords flow through the network unencrypted.」

・Reference: 他ホストから接続するための設定

That’s all from the Gemba regarding PostgreSQL remote connection configuration.

【References】

PostgreSQL8.3 クライアント認証 - 読書と技術となんか色々のログ - 楽天ブログ(Blog)