php 连接postgresql,PHP连接PostgreSQL连接问题

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

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

调试方法

[[email protected] ~]# 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

连接失败,不能连接到数据库[[email protected] ~]#

看起来是帐号密码的问题

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 数据库不存在

[[email protected] ~]# 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

连接失败,不能连接到数据库[[email protected] ~]#

修改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 "
php配置详细信息如下:";

echo phpinfo();

// 关闭连接

pg_close($dbconn);

?>

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值