CentOS5.xにPostgresqlをyumでインストールする

2010年7月6日

Filed under: CentOS,Linux,PostgreSQL — admin @ 10:57 PM

MySQLしか使ったことないのですが、案件でPostgresqlを使うことになりまして、CentOS5系にインストールしてみました。
クライアント認証につまづいてなかなか壁を突破できませんでしたがググって頑張りましたよっと。

まず、yumで必要なモジュールをインストール。

CODE:
  1. [root@localhost ~]# yum install -y postgresql-tcl postgresql-server postgresql-contrib postgresql php-pgsql

今回はPHPとの連動を行うので、下記もインストール

CODE:
  1. [root@localhost ~]# yum install -y php-pgsql

ひととおりインストールが終わったらpostgresqlを起動。

CODE:
  1. [root@localhost ~]# /etc/rc.d/init.d/postgresql start

それでは初期設定を行っていく。
MySQLと違って、postgresというユーザーでログインしないと設定が行えない。

CODE:
  1. [root@localhost ~]# su - postgres
  2. -bash-3.2$ psql -l
  3.         List of databases
  4.    Name    |  Owner   | Encoding
  5. -----------+----------+----------
  6.  postgres  | postgres | UTF8
  7.  template0 | postgres | UTF8
  8.  template1 | postgres | UTF8
  9. (3 rows)

ユーザー名"test"、パスワード"test" のユーザーを作成する。
権限はデータベースの作成を可能にする。

CODE:
  1. -bash-3.2$ psql
  2. Welcome to psql 8.1.21, the PostgreSQL interactive terminal.
  3.  
  4. Type:  \copyright for distribution terms
  5.        \h for help with SQL commands
  6.        \? for help with psql commands
  7.        \g or terminate with semicolon to execute query
  8.        \q to quit
  9.  
  10. postgres=# CREATE USER "test" CREATEDB PASSWORD 'test' LOGIN;
  11. CREATE ROLE
  12. postgres=# \q
  13. -bash-3.2$ exit
  14. logout
  15.  
  16. [root@localhost ~]#

これで、Linux上のユーザー"test"からPostgresqlを操作できるようになる。
そしたら、"test"ユーザーが使えるデータベース"test"を作成する。
文字コードは"EUC_JP"にする。

CODE:
  1. [root@localhost ~]# su - test
  2. [test@localhost ~]# createdb test -E EUC_JP
  3. CREATE DATABASE
  4. [test@localhost ~]$ psql -l
  5.         List of databases
  6.    Name    |  Owner   | Encoding
  7. -----------+----------+----------
  8.  test      | test     | EUC_JP
  9.  postgres  | postgres | UTF8
  10.  template0 | postgres | UTF8
  11.  template1 | postgres | UTF8
  12. (4 rows)
  13. [test@localhost ~]$

ちなみにただ単に "creatdb test" とした場合、文字コードはデフォルトで"utf-8"になる。
データベースを削除する場合は、"drop データベース名" とすれば良い。

最後に、PHPなどと連携する場合は、クライアント認証ファイルを編集する必要がある。

CODE:
  1. [root@localhost ~]# vi /var/lib/pgsql/data/pg_hba.conf
  2. #local  all         all                               ident sameuser  #コメントアウト
  3. local   all         all                               trust
  4.  
  5. #host   all         all         127.0.0.1/32          ident sameuser  #コメントアウト
  6. host    all         all         127.0.0.1/32          password

postgresqlとapacheを再起動する。

CODE:
  1. [root@localhost ~]# /etc/rc.d/init.d/postgresql restart
  2. Stopping postgresql service:                               [  OK  ]
  3. Starting postgresql service:                               [  OK  ]
  4.  
  5. [root@localhost ~]# /etc/rc.d/init.d/httpd restart
  6. Stopping httpd:                                            [  OK  ]
  7. Starting httpd:                                            [  OK  ]

phpファイルを書いて、接続テストする。

PHP:
  1. <?php
  2.  
  3. $data = "host=localhost user=test dbname=test password=test";
  4. $pg = pg_connect($data);
  5.  
  6. if(!$pg){
  7.   echo '接続できませんでした。';
  8. }
  9. else{
  10.   echo '接続しちゃいました<br />';
  11. }
  12.  
  13. if(!pg_close($pg)){
  14. }
  15. else{
  16.   echo '接続を閉じました。';
  17. }
  18.  
  19. ?>

特に問題なく接続できたら下記のように表示される。

CODE:
  1. 接続しちゃいました
  2. 接続を閉じました。

クライアント認証んとこは本当にわかんなくてつまづいたけど、ちゃんと繋がると感動ですね!