orz.conf

技術メモ✍

CentOS7にpostgresをインストール

CentOS7にpostgresql-9.6をインストールしてクライアント(OSはWin10 DB接続ツールはA5:SQL MK2)からアクセスしてみました。
CentOS7とクライアントは同セグメントです。

rpmパッケージをインストール

wget https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm
rpm -ivh pgdg-centos96-9.6-3.noarch.rpm

続いてyumにて下記をインストール
rpmパッケージインストール後でないと下記のコマンドでは何もインストールされませんでした。

yum -y install postgresql96-server postgresql96-devel postgresql96-contrib

セットアップファイルを検索

find / -name "postgresql*-setup*" -print

下記場所が出力される

/usr/pgsql-9.6/bin/postgresql96-setup

dbinitを実行

/usr/pgsql-9.6/bin/postgresql96-setup initdb

postgresユーザが作成されるので、必要であればパスワード変更。

コンフィグファイルの修正(場所はfindコマンドで検索)
pg_hba.confの編集
IPv4 local connectionsにローカルエリアを追記

# TYPE  DATABASE        USER            ADDRESS                 METHOD
# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
host    all             all             192.168.0.0/16          ident
# IPv6 local connections:
host    all             all             ::1/128                 ident
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local   replication     postgres                                peer
#host    replication     postgres        127.0.0.1/32            ident
#host    replication     postgres        ::1/128                 ident

postgresql.confの編集
中頃にCONNECTIONS AND AUTHENTICATIONがあるのでリッスンアドレスやポートを設定する

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'          # what IP address(es) to listen on;
#listen_addresses = 'localhost'         # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5432                             # (change requires restart)
max_connections = 100                   # (change requires restart)
#superuser_reserved_connections = 3     # (change requires restart)
#unix_socket_directories = '/var/run/postgresql, /tmp'  # comma-separated list of directories
                                        # (change requires restart)
#unix_socket_group = ''                 # (change requires restart)
#unix_socket_permissions = 0777         # begin with 0 to use octal notation
                                        # (change requires restart)
#bonjour = off                          # advertise server via Bonjour
                                        # (change requires restart)
#bonjour_name = ''                      # defaults to the computer name
                                        # (change requires restart)

起動する。

systemctl start postgresql-9.6

外部から接続出来るようにポートが開いているかどうか確認
テスト環境なのでどこからでもアクセスおk。

netstat -antu
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN

ファイアーウォールの許可

firewall-cmd --add-service=postgresql --zone=public --permanent
systemctl restart firewalld

postgresの初期設定

# su postgres
$ psql

DBが持っているユーザ postgresのパスワードを変更

postgres=# alter role postgres with password 'パスワード';

テスト接続用DBを作成

postgres=# CREATE DATABASE TEST;

クライアントから接続してみます。

接続出来なかったらログを参照。

tail -f /var/lib/pgsql/9.6/data/pg_log/postgresql-Sun.log
< 2017-04-23 13:49:24.699 JST > FATAL:  ユーザ"postgres"のパスワード認証に失敗しました
< 2017-04-23 13:49:24.699 JST > 詳細:  User "postgres" has no password assigned.
        接続はpg_hba.confの行83に一致しました: "host    all             all             192.168.0.0/16          md5"

上で設定したDBが持っているユーザ postgresのパスワード変更を
してないと上記のように出力されていました。

使用するデータベースに接続

postgres=# \connect TEST

テスト用テーブル作成

CREATE TABLE testTbl
  (No INT NOT NULL,
   username varchar(100),
   create_datetime timestamp,
   PRIMARY KEY(No, username));

テスト用データインサート

insert into testTbl values (1, "テスト", current_timestamp);

クライアントからデータ取得出来るか確認できれば完了です。