PHP连接PostgreSQL连接问题

 

/var/lib/pgsql/data/pg_hba.conf

 

CentOS 6.3下PostgreSQL 的安装与配置

http://www.cnblogs.com/mchina/archive/2012/06/06/2539003.html

postgresAdmin3

http://www.cnblogs.com/mchina/archive/2012/06/06/2539003.html

 

http://jingyan.baidu.com/article/3ea51489ec3cb452e71bba52.html

 

http://developer.51cto.com/art/201401/426225.htm

 

调试方法

[root@linux ~]# php /var/www/html/pgsql.php
PHP Warning:  pg_connect(): Unable to connect to PostgreSQL server: FATAL:  Ident authentication failed for user "postgres" in /var/www/html/pgsql.php on line 3
连接失败,不能连接到数据库[root@linux ~]#

看起来是帐号密码的问题

PostgreSQL 数据库默认会创建一个postgres的数据库用户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码,这里设定为’postgres’。

 

# su - postgres

 

$ psql

 

postgres=# ALTER USER postgres WITH PASSWORD ‘postgres‘;
ALTER ROLE
postgres=# select * from pg_shadow ;
 usename  | usesysid | usecreatedb | usesuper | usecatupd |               passwd
                | valuntil | useconfig
----------+----------+-------------+----------+-----------+---------------------
----------------+----------+-----------
 dbuser   |    16384 | f           | f        | f         | md5baa6c789c3728a1a4
49b82005eb54a19 |          |
 postgres |       10 | t           | t        | t         | md53175bce1d3201d165
94cebf9d7eb3f9d |          |
(2 rows)

postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileg
es   
-----------+----------+----------+-------------+-------------+------------------
-----
 david     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                             : postgres=CTc/post
gres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                             : postgres=CTc/post
gres
 testdb    | dbuser   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/dbuser
                                                             : dbuser=CTc/dbuser
(5 rows)

postgres=# ALTER USER dbuser WITH PASSWORD ‘dbuser‘;
ALTER ROLE
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileg
es   
-----------+----------+----------+-------------+-------------+------------------
-----
 david     | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                             : postgres=CTc/post
gres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
                                                             : postgres=CTc/post
gres
 testdb    | dbuser   | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/dbuser
                                                             : dbuser=CTc/dbuser
(5 rows)

不过仍然有Ident authentication failed for user错误,

尝试修改 /var/lib/pgsql/data/pg_hba.conf, 去掉

#host    all         all         127.0.0.1/32          ident


service postgresql restart

service httpd restart

service postgresql restart

重新执行php,发现这次dbuser能访问了,只是dbuser 数据库不存在

[root@linux ~]# php /var/www/html/pgsql.php
PHP Warning:  pg_connect(): Unable to connect to PostgreSQL server: FATAL:  database "dbuser" does not exist in /var/www/html/pgsql.php on line 3
连接失败,不能连接到数据库[root@linux ~]#

 

修改php脚本:

<?php
// 连接,选择数据库
$dbconn = pg_connect("host=127.0.0.1 dbname=david user=dbuser password=dbuser");
//see if our connection was successful
if (!$dbconn) {
    //connection failed - exit the page with an error
    //you could also try to proceed without the
    //database - it‘s up to you
     echo "连接失败,不能连接到数据库";
    exit;
}else{
    echo "连接成功";
}
echo "<br>php配置详细信息如下:";
echo phpinfo();
// 关闭连接
pg_close($dbconn);
?>

这次命令行执行php 可以成功显示数据,但是用firefox执行该脚本,仍然显示error , Why?

 

PHP连接PostgreSQL连接问题,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。