PostgreSQLでクライアント接続の設定をする

Linux(Debian)にインストールしたPostgreSQLに、Windows10からクライアント接続するためには、PostgreSQLのconfファイルを設定する必要があります。最初は、postgresql.confpg_hba.confがどこにあるかすら分からなかったので、場所の調べ方と設定方法を説明します。

設定ファイル

postgresql.confPostgreSQL全体の動作を制御
pg_hba.confクライアントからの接続を制御
pg_ident.confident認証とGSSAPI認証で使用

この設定ファイルの中で、postgresql.confとpg_hba.confの設定をしていきます。

今回使用するサーバーとクライアントの情報はこのようになっています。

OSIPアドレス
サーバー(PostgreSQL)Debian Buster192.168.111.111
クライアントWindows10 Pro192.168.111.222

設定ファイルの場所

まずは、設定ファイルがどこにあるか調べます。

PostgreSQLにpostgresユーザーでログインします。

sudo -u postgres psql

postgresql.conf

show config_file;
postgres=# show config_file;
               config_file               
-----------------------------------------
 /etc/postgresql/11/main/postgresql.conf

pg_hba.conf

show hba_file;
postgres=# show hba_file;
              hba_file               
-------------------------------------
 /etc/postgresql/11/main/pg_hba.conf

pg_ident.conf

show ident_file;
postgres=# show ident_file;
              ident_file               
---------------------------------------
 /etc/postgresql/11/main/pg_ident.conf

データディレクトリ

show data_directory;
postgres=# show data_directory;
       data_directory        
-----------------------------
 /var/lib/postgresql/11/main

postgresql.confの設定

postgresql.confでは、PostgreSQL全体の動作を制御します。

接続するクライアントのIPアドレス制限などは、pg_hba.confで制御します。

postgresql.conf
     53 #------------------------------------------------------------------------------
     54 # CONNECTIONS AND AUTHENTICATION
     55 #------------------------------------------------------------------------------
     56 
     57 # - Connection Settings -
     58 
     59 #listen_addresses = 'localhost'         # what IP address(es) to listen on;
     60                                         # comma-separated list of addresses;
     61                                         # defaults to 'localhost'; use '*' for all
     62                                         # (change requires restart)
     63 port = 5432                             # (change requires restart)
     64 max_connections = 100                   # (change requires restart)


デフォルトでは、localhostからの接続しか出来ないので、接続を受け付けるIPアドレスを追記します。追記するのは、クライアント(Windows)ではなく、サーバー(Debian)のIPアドレスです。

今回のサーバーIPアドレス192.168.111.111に限定する場合は、

listen_addresses = '192.168.111.111'

すべてのIP接続を許可する場合は、

listen_addresses = '*'

設定を反映させるために、PostgreSQLを再起動します。

sudo systemctl restart postgresql

listen_addressesの設定値

listen_addressesの設定値接続許可の対象接続方式
localhostローカル接続host
*すべてのIP接続host/hostssl/hostnossl
0.0.0.0すべてのIPv4アドレスからのIP接続host/hostssl/hostnossl
::すべてのIPv6アドレスからのIP接続host/hostssl/hostnossl

pg_hba.confの設定

pg_hba.confでは、クライアント(今回は、Windows)からの接続を制御します。

  1. local / host
  2. データベース
  3. ユーザー
  4. IPアドレス
  5. 認証方法
pg_hba.conf
     84 # Database administrative login by Unix domain socket
     85 local   all             postgres                                peer
     86 
     87 # TYPE  DATABASE        USER            ADDRESS                 METHOD
     88 
     89 # "local" is for Unix domain socket connections only
     90 local   all             all                                     md5
     91 # IPv4 local connections:
     92 host    all             all             127.0.0.1/32            md5
     93 # IPv6 local connections:
     94 host    all             all             ::1/128                 md5
     95 # Allow replication connections from localhost, by a user with the
     96 # replication privilege.
     97 local   replication     all                                     peer
     98 host    replication     all             127.0.0.1/32            md5
     99 host    replication     all             ::1/128                 md5

IPv4で接続する場合は、92行目の後に追記していきます。

書き方は、こんな感じです。

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5

# クライアントIPアドレスを 192.168.111.222 に限定 md5認証
host	all				all				192.168.111.222/32		md5

# クライアントIPアドレスを 192.168.111.1 から 192.168.111.254 に限定 md5認証
host	all				all				192.168.111.0/24		md5

# クライアントIPアドレスを 192.168.111.222 に限定 md5認証
# 接続するデータベースを testdb 、ユーザーを dattesar に限定
host	testdb			dattesar		192.168.111.222/32		md5

設定を反映させるために、PostgreSQLを再起動します。

sudo systemctl restart postgresql

クライアントから接続確認

ユーザー名dattesar
サーバーのIPアドレス192.168.111.111
ポート番号5432
データベース名testdb

PowerShellで入力

PowerShellでの操作
psql -U dattesar -h 192.168.111.111 -p 5432 -d testdb

接続できたら、ユーザーのパスワード入力を求められます。

postgresユーザーで接続する場合は、事前にpostgresユーザーのパスワードを設定しておく必要があります。

補足:PostgreSQLの操作

postgresユーザーにパスワードを設定

postgresユーザーでログインしてパスワードを設定します。

sudo -u postgres psql
\password
postgres=# \password
新しいパスワードを入力してください: 
もう一度入力してください:

ユーザーの作成

データベースとユーザー(ロール)を作成可能なユーザー(ユーザー名:dattesar パスワード:dattesarpass)を作成します。

create role dattesar with createdb createrole login password 'dattesarpass';
postgres=# create role dattesar with createdb createrole login password 'dattesarpass';
CREATE ROLE
postgres=# \du
                                              ロール一覧
 ロール名  |                                    属性                                    | 所属グループ 
-----------+----------------------------------------------------------------------------+--------------
 dattesar  | ロール作成可, DB作成可                                                     | {}