CentOS 6.4にPostgreSQL 9.4をインストールして使うところまで

PostgreSQLを使う必要があって手順を調べていたのでメモしておく。

インストール

yumでインストールできる。手順はpostgresqlWikiに書かれている。
YUM Installation - PostgreSQL wiki
1. /etc/yum.repos.d/CentOS-Base.repoの [base] と [updates] のセクションにexcludeを追記

exclude=postgresql*

2. pgdgをインストールする

sudo yum localinstall http://yum.postgresql.org/9.4/redhat/rhel-6-x86_64/pgdg-centos94-9.4-1.noarch.rpm

3. postgresql9.4をインストールする

sudo yum install postgresql94-server

データディレクトリの作成

サーバーを起動する前に初期化する。

sudo service postgresql-9.4 initdb

サービスを起動

sudo service postgresql-9.4 start

ロールの設定とDB作成

postgresユーザーでpsqlをまずは起動

sudo su - postgres
psql

tokibitoロールをパスワード無しで作成。権限はCREATEDB。

postgres=# CREATE ROLE tokibito WITH LOGIN CREATEDB;

tokibitoユーザーでシェルからcreatedbコマンドでデータベース作成

createdb tokibito

これでtokibitoユーザーでpsqlコマンドでtokibitoという名前のDBを使える。