码迷,mamicode.com
首页 > 数据库 > 详细

PHP连接PostgreSQL连接问题

时间:2014-06-28 11:20:25      阅读:375      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   数据   2014   

 

/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连接问题,布布扣,bubuko.com

PHP连接PostgreSQL连接问题

标签:style   blog   http   color   数据   2014   

原文地址:http://www.cnblogs.com/kylegui/p/3799027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!