S_a_k_Uの日記みたいなDB

~サクゥーと呼ばないで~

開発用サーバの構築(PostgreSQL)

いくつか嵌った箇所があるので、久々にブログ書くネタができたw
諸事情でCentOS 6.2(Minimal)にPostgreSQL 9.6をインストール。

CentOS の各種設定

  • ホスト名の設定
# vi /etc/sysconfig/network
========
HOSTNAME=hoge.fuga
========
  • ネットワークの設定
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
========
ONBOOT="yes"
TYPE="Ethernet"
BOOTPROTO="none"
IPADDR=xxx.xxx.xxx.xxx
NETMASK=xxx.xxx.xxx.xxx
GATEWAY=xxx.xxx.xxx.xxx
DNS1=xxx.xxx.xxx.xxx
DNS2=xxx.xxx.xxx.xxx
DNS3=xxx.xxx.xxx.xxx
========
  • プロキシの設定
# vi /etc/profile
========
PROXY='hoge.fuga:99999'
export http_proxy=$PROXY
export HTTP_PROXY=$PROXY
export https_proxy=$PROXY
export HTTPS_PROXY=$PROXY
========
  • ネットワークの再起動
# service network restart
  • wgetのインストール
# yum install wget
  • プロキシの設定(yum)
# vi /etc/profile
========
proxy=http://hoge.fuga:99999/
========
  • ネットワーク・セキュリティサービス(nss)のアップデート
# yum install -y nss

このnssのアップデートをしないと、yumで以下のようなエラーとなり、PostgreSQLリポジトリにアクセスできない。

# yum --enablerepo=pgdg96 list | grep postgres
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
base                                                     | 3.7 kB     00:00     
extras                                                   | 3.4 kB     00:00     
https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/repodata/repomd.xml: [Errno 14] problem making ssl connection
Trying other mirror.
Error: Cannot retrieve repository metadata (repomd.xml) for repository: pgdg96. Please verify its path and try again

<参考>仮想マシンPostgreSQLをインストールしたい。

PostgreSQL のインストールとDB作成

# wget https://yum.postgresql.org/9.6/redhat/rhel-6.7-x86_64/pgddg-centos96-9.6-3.noarch.rpm
# vi /etc/yum.repos.d/CentOS-Base.repo
========
([base]セクションに以下を追加)
exclude=postgresql*
========
([updates]セクションに以下を追加)
exclude=postgresql*
========
# yum --enablerepo=pgdg96 install postgresql96-server
# psql --version
psql (PostgreSQL) 9.6.6
  • データベースの初期化
# service postgresql-9.6 initdb

initdbで--locale=C(--no-locale)を指定すると、以下のようなエラーとなったため、データベースクラスタではCロケールの設定を行わず、DBに対してCロケールを設定する対応とした。
エラーとしては、pgstartup.logに以下のようなエラーが記録される。

initdb: ロケール名"--no-locale"は無効です。
(または、initdb: ロケール名"--locale=C"は無効です。)

なお、以下のコマンドで初期化すると、オプションの環境変数が有効とならず、デフォルトのlocaleが設定された。

# export PGSETUP_INITDB_OPTIONS="--no-locale --encoding=UTF-8"
(または、# export PGSETUP_INITDB_OPTIONS="--locale=C --encoding=UTF-8")
# /usr/pgsql-9.6/bin/initdb -D /var/lib/pgsql/9.6/data
  • 接続許可の設定

とりあえず全てのホストからの接続を許可する。

# vi /var/lib/pgsql/9.6/data/postgresql.conf
========
listen_addresses='*'
port = 5432
========
# vi /var/lib/pgsql/9.6/data/pg_hba.conf
========
host    all             all             0.0.0.0/0                 trust
========
# service postgresql-9.6 start
# chkconfig --list postgresql-9.6
postgresql-9.6  0:off   1:off   2:off   3:off   4:off   5:off   6:off
# chkconfig postgresql-9.6 on
# chkconfig --list postgresql-9.6
postgresql-9.6  0:off   1:off   2:on    3:on    4:on    5:on    6:off
# vi /etc/sysconfig/iptables	
========
-A INPUT -p tcp --dport 5432 -j ACCEPT
========
# /etc/rc.d/init.d/iptables restart	
  • 接続ユーザとDBの作成

管理者ユーザpostgresで実行する。
createdbでオプション--template=template0を付けないと、データベースの初期化(initdb)時と同じエラーとなる。

# su postgres
# createuser -P test
# createdb testdb --locale=C --template=template0 --owner="test"



# psql -l
                                   List of databases
    Name    |   Owner    | Encoding |   Collate   |    Ctype    |   Access privileges
------------+------------+----------+-------------+-------------+-----------------------
 postgres   | postgres   | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 |
 template0  | postgres   | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
            |            |          |             |             | postgres=CTc/postgres
 template1  | postgres   | UTF8     | ja_JP.UTF-8 | ja_JP.UTF-8 | =c/postgres          +
            |            |          |             |             | postgres=CTc/postgres
 testdb     | tesdb      | UTF8     | C           | C           |
(4 rows)

JDBCで接続できることを確認して完了。

カラムsettingがデータベースクラスタロケール
「lc_collate」と「lc_ctype」は変更できないが、「lc_messages」と「lc_monetary」と「lc_numeric」と「lc_time」は、postgresql.confで設定可能。

# su postgres
# psql
psql (9.6.6)
Type "help" for help.

postgres=# SELECT name, setting, context FROM pg_settings WHERE name LIKE 'lc%';
    name     |   setting   |  context
-------------+-------------+-----------
 lc_collate  | ja_JP.UTF-8 | internal
 lc_ctype    | ja_JP.UTF-8 | internal
 lc_messages | ja_JP.UTF-8 | superuser
 lc_monetary | ja_JP.UTF-8 | user
 lc_numeric  | ja_JP.UTF-8 | user
 lc_time     | ja_JP.UTF-8 | user
(6 rows)

<参考>PostgreSQL ロケール