thinkphp链接pgsql

来源:互联网 发布:刘雨欣张檬事件知乎 编辑:程序博客网 时间:2024/06/05 19:05

步骤:

1.(PHP扩展及设置-PHP扩展)中选中php-pgsql

2.PostgreSQL数据库的安装目录下找到/data/pg_hba.conf,找到“# IPv4 local connections:”

在其下加上请求连接的机器IP

host    all            all            xxx.xxx.xxx.xxx/32            md5


问题解决过程:

1.php版本问题,可能缺少相关的.dll文件,由php5.3切换至5.5n就不存在问题了(解决系统不支持pgsql问题)

Extension(PHP扩展及设置-PHP扩展)中选中php-pgsql

2.直接利用工程下的index.php文件进行链接(而非通过config.php文件)注释thinkphp所有相关,只有以下代码,替换后出现连接不成功的错误。(FATAL: no pg_hba.conf entry for host "192.168.0.123", user"postgres", database "postgres" FATAL: no pg_hba.conf entryfor host "192.168.0.123", user "postgres", database"postgres"

· <?php  

·    $host        = "host=xxx.xxx.xxx.xxx";  

·    $port        = "port=5432";  

·    $dbname      = "dbname=postgres";  

·    $credentials = "user=postgres password=postgres";  

·    echo $host;  

·   

·    $db = pg_connect( "$host $port $dbname $credentials");  

·    if(!$db){  

·       echo "Error : Unable to connect PostgreSQL\n";  

·    } else {  

·       echo "connect PostgreSQL successfully\n";  

·    }  

· ?>  

3.以上问题查询可知是pgsql安全访问问题。

PostgreSQL数据库为了安全,它不会监听除本地以外的所有连接请求,当用户通过JDBC访问是,会报一些如下的异常:

org.postgresql.util.PSQLException:FATAL: no pg_hba.conf entry for host

 

要解决这个问题,只需要在PostgreSQL数据库的安装目录下找到/data/pg_hba.conf,找到“# IPv4 local connections:”

在其下加上请求连接的机器IP

host allall 127.0.0.1/32 md5

32是子网掩码的网段;md5是密码验证方法,可以改为trus

 

4.最后成功

# IPv4local connections:

host    all             all             xxx.xxx.xxx/32            md5

直接copy文件上面的,然后把127.0.0.1替换成xxx.xxx.xxx.xxx(自己的ip),不要因为没有对齐删除空格,会出错。


0 0