<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ひろぽっぽれす &#187; PostgreSQL</title>
	<atom:link href="http://hiropo.co.uk/archives/category/postgresql/feed" rel="self" type="application/rss+xml" />
	<link>http://hiropo.co.uk</link>
	<description>Web系エンジニアの適当なブログです。どうでも酔いものばかり作ってます。</description>
	<lastBuildDate>Mon, 06 Feb 2012 05:45:41 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>CentOS5.xにPostgresqlをyumでインストールする</title>
		<link>http://hiropo.co.uk/archives/487</link>
		<comments>http://hiropo.co.uk/archives/487#comments</comments>
		<pubDate>Tue, 06 Jul 2010 13:57:14 +0000</pubDate>
		<dc:creator>hirohi</dc:creator>
				<category><![CDATA[CentOS]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PostgreSQL]]></category>

		<guid isPermaLink="false">http://hiropo.co.uk/?p=487</guid>
		<description><![CDATA[MySQLしか使ったことないのですが、案件でPostgresqlを使うことになりまして、CentOS5系にインストールしてみました。 クライアント認証につまづいてなかなか壁を突破できませんでしたがググって頑張りましたよっ &#8230; <a href="http://hiropo.co.uk/archives/487">続きを読む <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>MySQLしか使ったことないのですが、案件でPostgresqlを使うことになりまして、CentOS5系にインストールしてみました。<br />
クライアント認証につまづいてなかなか壁を突破できませんでしたがググって頑張りましたよっと。</p>
<p>まず、yumで必要なモジュールをインストール。</p>
<pre>
[root@localhost ~]# yum install -y postgresql-tcl postgresql-server postgresql-contrib postgresql php-pgsql
</pre>
<p>今回はPHPとの連動を行うので、下記もインストール</p>
<pre>
[root@localhost ~]# yum install -y php-pgsql
</pre>
<p>ひととおりインストールが終わったらpostgresqlを起動。</p>
<pre>
[root@localhost ~]# /etc/rc.d/init.d/postgresql start
</pre>
<p>それでは初期設定を行っていく。<br />
MySQLと違って、postgresというユーザーでログインしないと設定が行えない。</p>
<pre>
[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)
</pre>
<p>ユーザー名&#8221;test&#8221;、パスワード&#8221;test&#8221; のユーザーを作成する。<br />
権限はデータベースの作成を可能にする。</p>
<pre>
-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 ~]#
</pre>
<p>これで、Linux上のユーザー&#8221;test&#8221;からPostgresqlを操作できるようになる。<br />
そしたら、&#8221;test&#8221;ユーザーが使えるデータベース&#8221;test&#8221;を作成する。<br />
文字コードは&#8221;EUC_JP&#8221;にする。</p>
<pre>
[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 ~]$
</pre>
<p>ちなみにただ単に &#8220;creatdb test&#8221; とした場合、文字コードはデフォルトで&#8221;utf-8&#8243;になる。<br />
データベースを削除する場合は、&#8221;drop データベース名&#8221; とすれば良い。</p>
<p>最後に、PHPなどと連携する場合は、クライアント認証ファイルを編集する必要がある。</p>
<pre>
[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
</pre>
<p>postgresqlとapacheを再起動する。</p>
<pre>
[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  ]
</pre>
<p>phpファイルを書いて、接続テストする。</p>
<pre>
&lt;?php

$data = &quot;host=localhost user=test dbname=test password=test&quot;;
$pg = pg_connect($data);

if(!$pg){
  echo &#039;接続できませんでした。&#039;;
}
else{
  echo &#039;接続しちゃいました&lt;br /&gt;&#039;;
}

if(!pg_close($pg)){
}
else{
  echo &#039;接続を閉じました。&#039;;
}

?&gt;
</pre>
<p>特に問題なく接続できたら下記のように表示される。</p>
<pre>
接続しちゃいました
接続を閉じました。
</pre>
<p>クライアント認証んとこは本当にわかんなくてつまづいたけど、ちゃんと繋がると感動ですね！</p>
]]></content:encoded>
			<wfw:commentRss>http://hiropo.co.uk/archives/487/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

