はじめに
PostgreSQL 初心者です。
最近ちらほらと PostgreSQL を使うようになったのですが、いちいち手でインストールするのは面倒なので、さっくりと自動化したいと考えていました。
そこで 前回 紹介した、Ansible Galaxy です。
ローカルに Ansible がインストールされていれば、わずか 3分で PostgreSQL をサーバにインストール することができます。
<過去記事>
akiyoko.hatenablog.jp
なお、今回利用した Role は、
https://galaxy.ansible.com/detail#/role/512
で、Ubuntuサーバに PostgreSQL をインストールするための Role です。
利用できる変数などの詳細については、こちら を参照してください。

環境
<クライアント>
- Mac OS X 10.10.5
- ansible 1.9.3
<サーバ>
- Ubuntu 14.04 LTS (on Vagrant)
- IPアドレス : 192.168.33.10
PostgreSQL インストール
以下のコマンドを、クライアント上(私の場合は Mac)で実行します。
$ mkdir -p ~/dev/ansible-postgresql && cd $_
### ANXS.postgresql を Ansible Galaxy からインストール
$ ansible-galaxy install ANXS.postgresql -p roles
### Inventoryファイル(hosts)を作成
$ cat << EOF > hosts
[db-servers]
192.168.33.10
EOF
### Playbookファイル(site.yml)を作成
$ cat << EOF > site.yml
- hosts: db-servers
user: vagrant
vars_files:
- vars/database.yml
roles:
- { role: ANXS.postgresql }
EOF
### Variablesファイル(vars/database.yml)を作成
$ mkdir vars
$ cat << EOF > vars/database.yml
postgresql_databases:
- name: myproject
postgresql_users:
- name: myprojectuser
pass: myprojectuserpass
postgresql_user_privileges:
- name: myprojectuser
db: myproject
priv: "ALL"
EOF
### Playbookを実行
$ ansible-playbook -i hosts site.yml --sudo -k -vv上記のサンプルでは、PostgreSQL をインストールするのに合わせて、以下のデータベースとユーザを作成しています。
| 項目 | 設定内容 |
|---|---|
| データベース名 | myproject |
| データベースユーザ | myprojectuser |
| データベースユーザパスワード | myprojectuserpass |
なお、サーバのIPアドレスを変更したい場合は hosts、データベース名やデータベースユーザ名・パスワードを変更したい場合は vars/database.yml を適宜編集してから実行してください。
もしインストールの途中で失敗してしまう場合は、サーバから PostgreSQL を削除すれば、何度でも Playbook の実行をやり直すことができます。
$ sudo apt-get --purge -y autoremove postgresql*
<参考>
apt - Uninstall an specific version of PostgreSQL - Ask Ubuntu
上記のサンプルを、私の GitHub のリポジトリにアップしているので、それを使えば、たった数行のコマンドでインストールすることができます。要 git です。
$ mkdir -p ~/dev && cd $_ $ git clone https://github.com/akiyoko/ansible-postgresql.git $ cd ansible-postgresql/ $ ansible-playbook -i hosts site.yml --sudo -k -vv
<参考>
github.com
おまけ
psql コマンドの使い方
$ psql -U postgres or $ sudo su postgres $ psql
### データベース一覧(MySQL では show databases)
postgres=# \l
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+----------------------------
myproject | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres +
| | | | | postgres=CTc/postgres +
| | | | | myprojectuser=CTc/postgres
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
### ユーザ一覧(MySQLでは select * from mysql.user;)
postgres=# \du
List of roles
Role name | Attributes | Member of
---------------+------------------------------------------------+-----------
myprojectuser | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
or
postgres=# select * from pg_shadow;
usename | usesysid | usecreatedb | usesuper | usecatupd | userepl | passwd | valuntil | useconfig
---------------+----------+-------------+----------+-----------+---------+-------------------+----------+-----------
postgres | 10 | t | t | t | t | | |
myprojectuser | 16384 | f | f | f | f | myprojectuserpass | |
(2 rows)
### データベース接続(MySQL では use myproject)
postgres=# \c myproject
### テーブル一覧(MySQL では show tables)
postgres=# \d
Mac で使える PostgreSQLクライアント
pgAdmin III
brew cask install pgadmin3
動作が少しもっさりしてて、見た目もイケてないが、とりあえずこれで。
何かいいのがあれば乗り換えたい。。
PSequel
brew cask install psequel
SSH Tunnel 利用時に、パスワード認証が使えなかった。。
他にもいろいろ。
https://wiki.postgresql.org/wiki/Community_Guide_to_PostgreSQL_GUI_Tools