・パッケージをインストールする ※PHP対応必要があれば php-pgsqlもインストール
[root@centos ~]# yum -y install postgresql postgresql-server php-pgsql
・サービス開始、自動起動設定
[root@centos ~]# /etc/init.d/postgresql start
[root@centos ~]# chkconfig postgresql on
[root@centos ~]# chkconfig --list postgresql
・postgresユーザ確認・パスワード設定
[root@centos ~]# id postgres
uid=26(postgres) gid=26(postgres) 所属グループ=26(postgres)
[root@centos ~]# passwd postgres
・psql接続/データベース確認
[root@centos ~]# su - postgres
-bash-3.2$ psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
(3 rows)
・ユーザ確認
-bash-3.2$ psql
Welcome to psql 8.1.18, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
postgres=# \du
List of roles
Role name | Superuser | Create role | Create DB | Connections | Member of
-----------+-----------+-------------+-----------+-------------+-----------
postgres | yes | yes | yes | no limit |
(1 row)
・外部より接続制限設定
※pg_hba.confの詳細はこちら
[root@centos ~]# su - postgres
-bash-3.2$ vi /var/lib/pgsql/data/postgresql.conf
#listen_addresses = 'localhost' # what IP address(es) to listen on;
↓
listen_addresses = '*' # what IP address(es) to listen on;
-bash-3.2$ vi /var/lib/pgsql/data/pg_hba.conf
※最終へ追加
local all all trust
※↑ ローカルからのアクセスは無条件許可※危険
host all all 192.168.1.0/24 trust
※↑ 192.168.1.0/24からのアクセスは無条件許可
host all all 0.0.0.0/0 password
※↑ 上記以外のすべてはpasswordより認証が必要
-bash-3.2$ exit
logout
[root@centos ~]# /etc/init.d/postgresql restart
postgresql サービスを停止中: [ OK ]
postgresql サービスを開始中: [ OK ]
[root@centos ~]#
・新規ユーザ作成 ※Linuxのユーザーは一致する必要がある
[root@centos ~]# useradd testdb
[root@centos ~]# passwd testdb
[root@centos ~]# createuser -U postgres -P testdb
Enter password for new role:
Enter it again:
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
CREATE ROLE
・作成ユーザを確認
[root@centos ~]# su - postgres
-bash-3.2$ psql
Welcome to psql 8.1.18, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
postgres-# \du
List of roles
Role name | Superuser | Create role | Create DB | Connections | Member of
-----------+-----------+-------------+-----------+-------------+-----------
postgres | yes | yes | yes | no limit |
testdb | no | yes | yes | no limit |
(2 rows)
postgres-# \q
-bash-3.2$ exit
logout
・データベース新規作成
※ユーザとデータベースの名前を統一すると管理しやすい
[root@centos ~]# createdb -E UTF-8 -O testdb -U testdb testdb
CREATE DATABASE
※オプション
-E [エンコーディング名] 文字エンコードを指定
-O [オーナ名] 所有者となるユーザを指定
-U [接続ユーザ名] PostgreSQLに接続するユーザ名を指定
・作成したデータベースを確認
[root@centos ~]# su - postgres
-bash-3.2$ psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
testdb | testdb | UTF8
(4 rows)
・接続テスト
[root@centos ~]# psql testdb -U testdb -W
Password for user testdb:
Welcome to psql 8.1.18, the PostgreSQL interactive terminal.
Type: \copyright for distribution terms
\h for help with SQL commands
\? for help with psql commands
\g or terminate with semicolon to execute query
\q to quit
testdb=>
最近のコメント