MySQLしか使ったことないのですが、案件でPostgresqlを使うことになりまして、CentOS5系にインストールしてみました。
クライアント認証につまづいてなかなか壁を突破できませんでしたがググって頑張りましたよっと。
まず、yumで必要なモジュールをインストール。
CODE:
-
[root@localhost ~]# yum install -y postgresql-tcl postgresql-server postgresql-contrib postgresql php-pgsql
今回はPHPとの連動を行うので、下記もインストール
CODE:
-
[root@localhost ~]# yum install -y php-pgsql
ひととおりインストールが終わったらpostgresqlを起動。
CODE:
-
[root@localhost ~]# /etc/rc.d/init.d/postgresql start
それでは初期設定を行っていく。
MySQLと違って、postgresというユーザーでログインしないと設定が行えない。
CODE:
-
[root@localhost ~]# su - postgres
-
-bash-3.2$ psql -l
-
List of databases
-
Name | Owner | Encoding
-
-----------+----------+----------
-
postgres | postgres | UTF8
-
template0 | postgres | UTF8
-
template1 | postgres | UTF8
-
(3 rows)
ユーザー名"test"、パスワード"test" のユーザーを作成する。
権限はデータベースの作成を可能にする。
CODE:
-
-bash-3.2$ psql
-
Welcome to psql 8.1.21, 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=# CREATE USER "test" CREATEDB PASSWORD 'test' LOGIN;
-
CREATE ROLE
-
postgres=# \q
-
-bash-3.2$ exit
-
logout
-
-
[root@localhost ~]#
これで、Linux上のユーザー"test"からPostgresqlを操作できるようになる。
そしたら、"test"ユーザーが使えるデータベース"test"を作成する。
文字コードは"EUC_JP"にする。
CODE:
-
[root@localhost ~]# su - test
-
[test@localhost ~]# createdb test -E EUC_JP
-
CREATE DATABASE
-
[test@localhost ~]$ psql -l
-
List of databases
-
Name | Owner | Encoding
-
-----------+----------+----------
-
test | test | EUC_JP
-
postgres | postgres | UTF8
-
template0 | postgres | UTF8
-
template1 | postgres | UTF8
-
(4 rows)
-
[test@localhost ~]$
ちなみにただ単に "creatdb test" とした場合、文字コードはデフォルトで"utf-8"になる。
データベースを削除する場合は、"drop データベース名" とすれば良い。
最後に、PHPなどと連携する場合は、クライアント認証ファイルを編集する必要がある。
CODE:
-
[root@localhost ~]# vi /var/lib/pgsql/data/pg_hba.conf
-
#local all all ident sameuser #コメントアウト
-
local all all trust
-
-
#host all all 127.0.0.1/32 ident sameuser #コメントアウト
-
host all all 127.0.0.1/32 password
postgresqlとapacheを再起動する。
CODE:
-
[root@localhost ~]# /etc/rc.d/init.d/postgresql restart
-
Stopping postgresql service: [ OK ]
-
Starting postgresql service: [ OK ]
-
-
[root@localhost ~]# /etc/rc.d/init.d/httpd restart
-
Stopping httpd: [ OK ]
-
Starting httpd: [ OK ]
phpファイルを書いて、接続テストする。
PHP:
-
<?php
-
-
$data = "host=localhost user=test dbname=test password=test";
-
-
-
if(!$pg){
-
-
}
-
else{
-
-
}
-
-
-
}
-
else{
-
-
}
-
-
?>
特に問題なく接続できたら下記のように表示される。
クライアント認証んとこは本当にわかんなくてつまづいたけど、ちゃんと繋がると感動ですね!