博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP连接PostgreSQL连接问题
阅读量:7006 次
发布时间:2019-06-28

本文共 3279 字,大约阅读时间需要 10 分钟。

 

/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

 

调试方法

[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?

 

转载于:https://www.cnblogs.com/kylegui/p/3799027.html

你可能感兴趣的文章
CountDownLatch和CyclicBarrier模拟同时并发请求
查看>>
下沉市场中的新零售,如何抓住“小镇青年”的心
查看>>
java web 人脸识别技术
查看>>
RabbitMQ 与 AMQP路由
查看>>
s闭包(转载)
查看>>
微软整合实验(二):Hyper-V安装和虚拟网络详解
查看>>
linux下解压命令大全
查看>>
Linux学习之CentOS(三十一)--Linux远程管理之SSH、VNC
查看>>
rsync 常见错误 - 总结
查看>>
无线数据采集器设计
查看>>
开通博客的第一天
查看>>
SQL 获取数据
查看>>
android 实现 JNI NDK 入门例子教程+源码
查看>>
微信小程序把玩(三十六)Storage API
查看>>
16次课( lvm讲解、 磁盘故障小案例)
查看>>
Windows Embedded Compact 7数据库开发(下)
查看>>
TypeScript 开发系列(一)——简介与简单创建
查看>>
优化网站架构,最大限度减少重复劳动
查看>>
BeanComparator实现ArrayList中的元素按多个属性复合排序
查看>>
对license数据的挖掘与分析
查看>>