본문 바로가기

프로그램 경험/PHP

PHP + PostgreSQL 연동

MAC 에는 기본적으로 PHP가 설치 되어 있다.
원격지에 있는 PostgreSQL을 사용하게 되어서 연동하려고 하려고 검색을 했는데
다들 PostgreSQL을 설치하고 PHP를 설치 하는 포스트 밖에 없다.

난 그저 드라이버만 설치하고 싶은데...
드라이버 설치 하는법이 있길래 해봤는데 오류...

그래서 MAPP를 설치하고 거기에 들어있는 pgsql.so를 복사하여 내 php extension 폴더에
넣고 php.ini를 에 extension=pgsql.so 한줄 넣으니 된다.

MAPP를 삭제 하니 다시 안된다... 뭔가 다른 방법을 찾아야 겠다.

PostgreSQL 소스를 받아서 설치 하고 PHP를 다운 받고 ext/pgsql 만 설치 하려하니까 다음과 같은 오류가 뜬다.

---------------------------------------------------------------
make: *** [pgsql.lo] Error 1
---------------------------------------------------------------

혹시나 해서 .bash_profile에 다음을 추가하고 PDO_PGSQL 설치 하니 된다.

---------------------------------------------------------------
export PGSQL_HOME=/usr/local/pgsql
PATH=$PATH:$PGSQL_HOME/lib
---------------------------------------------------------------
$>pecl install pdo_pgsql
---------------------------------------------------------------

이제 CodeIgniter에서 사용하는 일만 남은줄 알았는데...
내가 사용방법을 모르는 건지.. CodeIgniter 2.1.0 에 PDO_PGSQL 사용 인터페이스가 없는건지... 안된다.ㅜㅜ

그래서 CodeIgniter/system/database/drivers/pdo 폴더를 복사해서 pdo_pgsql로 변경 하고
안에 있는 파일들을 모두 pdo_ 라는 시작어를 pdo_pgsql 로 변경하고 안의 내용중 클래스 명칭도 바꿔주었다.
그리고 pdo_pgsql_driver.php의 생성자등 들은 다음과 같이 변경하고

---------------------------------------------------------------
        var $dbdriver = 'pdo_pgsql';
        ...(생략)
function __construct($params)
{
parent::__construct($params);
// // clause and character used for LIKE escape sequences
// if (strpos($this->hostname, 'mysql') !== FALSE)
// {
// $this->_like_escape_str = '';
// $this->_like_escape_chr = '';
//
// //Prior to this version, the charset can't be set in the dsn
// if(is_php('5.3.6'))
// {
// $this->hostname .= ";charset={$this->char_set}";
// }
//
// //Set the charset with the connection options
// $this->options['PDO::MYSQL_ATTR_INIT_COMMAND'] = "SET NAMES {$this->char_set}";
// }
// else if (strpos($this->hostname, 'odbc') !== FALSE)
// {
// $this->_like_escape_str = " {escape '%s'} ";
// $this->_like_escape_chr = '!';
// }
// else
// {
// $this->_like_escape_str = " ESCAPE '%s' ";
// $this->_like_escape_chr = '!';
// }
// $this->hostname .= ";dbname=".$this->database;
        $this->hostname = "pgsql:host=".$this->hostname.";dbname=".$this->database;
$this->trans_enabled = FALSE;

$this->_random_keyword = ' RND('.time().')'; // database specific random keyword
}
--------------------------------------------------------------- 

그리고 database.php 설정에서는 다음과 같이 넣어주고 사용하면 된다.

---------------------------------------------------------------
$db['default']['dbdriver'] = 'pdo_pgsql';
---------------------------------------------------------------

여기까지는 내 맥에서 설정하는거였고...
서버측에 있는 리눅스에서는 PostgreSQL이 설치가 안된다...ㅜㅜ

PostgreSQL 소스 코드를 다운받아서 풀고 ./configure를 하면 이런 오류가...

---------------------------------------------------------------
checking for library containing readline... no
configure: error: readline library not found

If you have readline already installed, see config.log for details on the
failure.  It is possible the compiler isn't looking in the proper directory.

Use --without-readline to disable readline support. 
---------------------------------------------------------------

그래서 readline 을 설치해주고 make && make install 하면 된다.
그리고 PDO_PGSQL을 설치 하면 된다.
---------------------------------------------------------------
$> yum install readline-devel
$> ./configure
$> make && make install
$> pecl install pdo_pgsql

---------------------------------------------------------------

 
 

'프로그램 경험 > PHP' 카테고리의 다른 글

[PHP] JSON 사용  (0) 2012.03.23
[PHP] 난수 생성  (0) 2012.03.22
[PHP] xml 데이터 submit 하기  (0) 2012.03.14
[PHP] 로그 남기기  (0) 2012.03.05
PHP로 소켓 서버 작성하기  (0) 2011.12.11