无法使用 PHP pg_connect() 连接到 PostgreSQL

2024-04-13php开发问题
9

本文介绍了无法使用 PHP pg_connect() 连接到 PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我刚刚意识到这个问题可能更适合 ServerFault.版主不要复制它,请把它移过来?谢谢.

I just realized that this question may be better suited to ServerFault. Instead of copying it, a moderator please move it over? Thanks.

我检查了 php-info,并且 Postgresql 扩展在那里(pg_connect() 不是未定义的).我还可以使用本地主机上的 psql 连接到 postgresql(我已经适当地编辑了我的 pg_hba.conf 文件).这是不起作用的代码:

I've checked php-info, and the Postgresql extension is there (pg_connect() is not undefined). I am also able to connect to postgresql using psql on localhost (I've edited my pg_hba.conf file appropriately). Here is the code that's not working:

<?php
$dbconn = pg_connect("host=localhost port=5432 dbname=mydb user=myuser password=mypass") or die('Could not connect: ' . pg_last_error());
?>

此代码只会导致浏览器中显示无法连接:".

This code simply results in "Could not connect: " being displayed in the browser.

我检查了我的 apache 日志,这是相关的错误消息:

I checked my apache log, and here's the relevant error message:

PHP Warning:  pg_connect() [<a href='function.pg-connect'>function.pg-connect</a>]: 
Unable to connect to PostgreSQL server: could not connect to server: Permission 
denied
	Is the server running on host &quot;localhost&quot; and accepting
	TCP/IP 
connections on port 5432?

如何修复/调试此问题?

How can I fix/debug this?

我在 Centos 5.4 顺便说一句.

I'm on Centos 5.4 btw.

推荐答案

无法连接到服务器:权限被拒绝

could not connect to server: Permission denied

我在 Centos 5.4 顺便说一句.

I'm on Centos 5.4 btw.

检查 /var/log/audit/audit.log.您可能遇到了 SELinux 规则.

Check /var/log/audit/audit.log. Chances are that you're hitting a SELinux rule.

这篇关于无法使用 PHP pg_connect() 连接到 PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17