开发者

"Unable to connect to PostgreSQL server" with pg_connect on localhost

开发者 https://www.devze.com 2023-01-14 12:01 出处:网络
I\'ve a server with postgresql installed on the postgres user. When I do: su - postgres [entering the password]

I've a server with postgresql installed on the postgres user. When I do:

su - postgres
[entering the password]
psql

I can then execute queries on the database, that works fine.

However, from my php script (running on my own account):

$dbconnection = pg_connect("host=localhost port=5432 user=postgres password=XXXXXXX ");

(password crossed out of course)

I then get the message:

PHP Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: password authentication failed for user "postgres" FATAL: password authentication failed for user "postgres" in /home/username/script.php on line 18

I tried a lot of variations on开发者_JS百科 the login string, but I keep getting the same message. Any ideas how I could try to solve this?


What is the current configuration for pg_hba.conf? And did you set a password for the database rol "postgres" ? su - postgres is for the Linux user "postgres", not for the database role "postgres". If you can start psql without entering the password, it looks like pg_hba.conf uses "trust" and not "password" of even better "md5". Or did you create a .pgpass file?


Your postgres may not listen on a network interface but on a filesystem socket in stead. What if you leave out host=/port= entirely?

Also, if you know the socketpath (and it's non-default), you may use that as host= argument


Be careful, the following (too short) connection string causes the mentioned error message ("Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL: Peer authentication failed for user "halmai" in..."):

pg_connect("dbname=halmai user=halmai password=halmai");

However, the more complete connection string works well:

pg_connect("host=localhost dbname=halmai user=halmai password=halmai");

My environment: php7.0.8.

0

精彩评论

暂无评论...
验证码 换一张
取 消