S-JIS[2020-09-08]
PostgreSQL12のインストールのメモ。
|
|
PostgreSQLをCentOSにインストールするには、yumコマンドを使用する。
ただし、CentOSのデフォルトのyumリポジトリーにはPostgreSQLは入っていないので、リポジトリーRPMを追加する必要がある。
どのリポジトリーRPMを追加するのかはOSのバージョンやCPUアーキテクチャー(x86かどうか)によって違うので、自分の環境によって変える。
アーキテクチャーの確認 # uname -m x86_64
該当するリポジトリーRPMをパッケージページから探す。
CentOS7のx86_64なら、https://yum.postgresql.org/reporpms/EL-7-x86_64/。
リポジトリーRPMのインストールは以下のようにして行う。
# yum -y install https://yum.postgresql.org/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
PostgreSQL12をインストールする。
# yum -y install postgresql12-server
これで、psqlコマンドが使えるようになる。
また、UNIXユーザーのpostgres(グループもpostgres)が作られる。
# su - postgres $ pwd /var/lib/pgsql $ ls 12 $ ls 12 backups data
DBのデータの実体は/var/lib/pgsql/12/dataになる。
データベースクラスターを作成する。(作成しないとPostgreSQLを起動できない)
initdbコマンドでデータベースクラスターを作成する。
-Wオプションを付けると、PostgreSQLのpostgresユーザー(Linuxのではない)のパスワードを登録できる。
# su - postgres $ /usr/pgsql-12/bin/initdb -E UTF8 --locale=C -W データベースシステム内のファイルの所有者はユーザ"postgres"となります。 このユーザをサーバプロセスの所有者とする必要があります。 データベースクラスタはロケール"C"で初期化されます。 デフォルトのテキスト検索構成は english に設定されます。 データベージのチェックサムは無効です。 新しいスーパユーザのパスワードを入力してください: 再入力してください: ディレクトリ/var/lib/pgsql/12/dataの権限を設定しています ... ok サブディレクトリを作成しています ... ok 動的共有メモリの実装を選択しています ... posix デフォルトのmax_connectionsを選択しています ... 100 デフォルトの shared_buffers を選択しています ... 128MB selecting default time zone ... Asia/Tokyo 設定ファイルを作成しています ... ok ブートストラップスクリプトを実行しています ... ok ブートストラップ後の初期化を実行しています ... ok データをディスクに同期しています ... ok initdb: 警告: ローカル接続に対して"trust"認証を有効にします pg_hba.confを編集する、もしくは、次回initdbを実行する時に -A オプション、 あるいは --auth-local および --auth-host オプションを使用することで変更する ことがきます。 成功しました。以下のようにしてデータベースサーバを起動することができます: /usr/pgsql-12/bin/pg_ctl -D /var/lib/pgsql/12/data -l ログファイル start
rootユーザーにて起動する。
# systemctl start postgresql-12.service # systemctl status postgresql-12.service ←起動確認
データベースが稼動しているかどうか確認するには、postgresユーザーでpsqlを実行してみる。
(データベースが稼動していないと、「psql -l
」が失敗する)
# su - postgres $ psql -V psql (PostgreSQL) 12.4 $ psql -l データベース一覧 名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限 -----------+----------+------------------+----------+-------------------+----------------------- postgres | postgres | UTF8 | C | C | template0 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres (3 行)
※psqlにpostgresユーザーを指定すれば、Linuxのpostgresユーザーにスイッチしなくても良い。「psql -U
postgres -l
」
サーバーを起動したときにPostgreSQLも自動的に起動するようにしておくには、以下のコマンドを実行しておけばいいらしい。
# systemctl enable postgresql-12.service
PostgreSQLでは、ユーザーとDBにはOracleの様な密接な関係は無いので、別々に作成する。
(ただ、同じ名前にしておくと、psqlの引数を省略できる)
# su - postgres $ createuser --login --pwprompt hishidama 新しいロールのためのパスワード: もう一度入力してください: $ createdb --owner hishidama hdb $ psql -l データベース一覧 名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限 -----------+-----------+------------------+----------+-------------------+----------------------- hdb | hishidama | UTF8 | C | C | postgres | postgres | UTF8 | C | C | template0 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | C | C | =c/postgres + | | | | | postgres=CTc/postgres (4 行)
↓作成したDBへのログイン
$ psql -U hishidama hdb
hdb=> \? ←ヘルプ表示
Linuxユーザー名とPostgreSQLユーザー名が同一の場合は、psqlの引数「-U ユーザー名」は省略できる。
デフォルトでは、TCP/IP(JDBCとか)による接続が許可されていない。
以前のバージョンのPostgreSQLではローカルホスト内(127.0.0.1)でのTCP/IPによる接続もデフォルトでは不許可だったが、
PostgreSQL12ではデフォルトで許可されているようだ。
# TYPE DATABASE USER ADDRESS METHOD # IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all ::1/128 trust
※以前のバージョンのPostgreSQLではidentだったので、trust(常に信頼する)に変更する必要があった。
CentOS7の場合、ファイアウォールで外部からのTCP接続が不許可になっているので、許可する必要がある。
# firewall-cmd --permanent --add-port=5432/tcp success # firewall-cmd --reload success # firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: ssh dhcpv6-client ports: 5432/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
ファイアウォールでTCP接続が許可されていない場合、外部からJDBC接続しようとするとタイムアウトで接続失敗する。
Exception in thread "main" org.postgresql.util.PSQLException: 接続試行は失敗しました。 at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:315) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) 〜 Caused by: java.net.SocketTimeoutException: connect timed out 〜
PostgreSQL12のデフォルトではpostmasterが外部からのTCP接続を受け付けてないので、許可する必要がある。
#listen_addresses = 'localhost' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost'; use '*' for all # (change requires restart) listen_addresses = '*' port = 5432
postgresql.confを変更した場合は、PostgreSQLを再起動しないと反映されない。
# systemctl restart postgresql-12.service
この許可がされていない場合、外部からJDBC接続しようとすると以下のようなエラーになる。
Exception in thread "main" org.postgresql.util.PSQLException: 192.168.1.2:5432 への接続が拒絶されました。ホスト名とポート番号が正しいことと、postmaster がTCP/IP接続を受け付けていることを確認してください。
また、許可する接続元をクライアント認証コンフィグファイル(pg_hba.conf)に登録する必要がある。
# TYPE DATABASE USER ADDRESS METHOD host hdb hishidama 192.168.1.1/32 md5
pg_hba.confを変更した場合は、PostgreSQLをリロード(または再起動)しないと反映されない。
# systemctl reload postgresql-12.service
この設定がされていない場合、外部からJDBC接続しようとすると以下のようなエラーになる。
Exception in thread "main" org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host "192.168.1.1", user "hishidama", database "hdb", SSL off