php can't connect to postgresql server

Started by Clodoaldo Pintoalmost 20 years ago8 messagesgeneral
Jump to latest
#1Clodoaldo Pinto
clodoaldo.pinto@gmail.com

php can't connect to postgresql server

php error log 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\n\tIs the server running on host &quot;127.0.0.1&quot; and
accepting\n\tTCP/IP connections on port 5432?

php connection string:

$conn_string = "hostaddr=127.0.0.1 port=5432 dbname=dbname
user=username password=password";

Also tried host=localhost

pg_hba:

host dbname username 127.0.0.1/32 md5

I can connect as that user with psql:

$ psql -h localhost -U username dbname
Password for user username:
Welcome to psql 8.1.4, the PostgreSQL interactive terminal.

This same setup works in another server running 8.0. Differences:

old server | new server
FC3 | FC5
php 4.3.11 | php 5.1.4
httpd 2.0 | httpd 2.2

What else should I check? I am out of ideas.

Regards, Clodoaldo Pinto

#2A. Kretschmer
andreas.kretschmer@schollglas.com
In reply to: Clodoaldo Pinto (#1)
Re: php can't connect to postgresql server

am 03.07.2006, um 10:25:29 -0300 mailte Clodoaldo Pinto folgendes:

$conn_string = "hostaddr=127.0.0.1 port=5432 dbname=dbname

^^^^^^^^^

I can connect as that user with psql:

$ psql -h localhost -U username dbname

^^^^^^^^^

Try 'localhost' instead '127.0.0.1' in your $conn_string.

My guess: pg don't listen on tcp/ip, only on the local socket.
(check listen_addresses in your postgresql.conf)

HTH, Andreas
--
Andreas Kretschmer (Kontakt: siehe Header)
Heynitz: 035242/47215, D1: 0160/7141639
GnuPG-ID 0x3FFF606C http://wwwkeys.de.pgp.net
=== Schollglas Unternehmensgruppe ===

#3Clodoaldo Pinto
clodoaldo.pinto@gmail.com
In reply to: A. Kretschmer (#2)
Re: php can't connect to postgresql server

2006/7/3, A. Kretschmer <andreas.kretschmer@schollglas.com>:

am 03.07.2006, um 10:25:29 -0300 mailte Clodoaldo Pinto folgendes:

$conn_string = "hostaddr=127.0.0.1 port=5432 dbname=dbname

^^^^^^^^^

I can connect as that user with psql:

$ psql -h localhost -U username dbname

^^^^^^^^^

Try 'localhost' instead '127.0.0.1' in your $conn_string.

As I already said I have tried localhost also.

My guess: pg don't listen on tcp/ip, only on the local socket.
(check listen_addresses in your postgresql.conf)

listen_addresses = '127.0.0.1,xx.xx.xxx.xx'

If I can connect with the -h localhost psql's option then I guess it
is listening to tcp/ip, isn't it?

Clodoaldo

#4Robert Treat
xzilla@users.sourceforge.net
In reply to: Clodoaldo Pinto (#1)
Re: php can't connect to postgresql server

On Monday 03 July 2006 09:25, Clodoaldo Pinto wrote:

php can't connect to postgresql server

php error log 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\n\tIs the server running on host &quot;127.0.0.1&quot; and
accepting\n\tTCP/IP connections on port 5432?

php connection string:

$conn_string = "hostaddr=127.0.0.1 port=5432 dbname=dbname
user=username password=password";

Also tried host=localhost

It needs to be host, not hostaddr.

pg_hba:

host dbname username 127.0.0.1/32 md5

I can connect as that user with psql:

$ psql -h localhost -U username dbname
Password for user username:
Welcome to psql 8.1.4, the PostgreSQL interactive terminal.

This same setup works in another server running 8.0. Differences:

old server | new server
FC3 | FC5
php 4.3.11 | php 5.1.4
httpd 2.0 | httpd 2.2

What else should I check? I am out of ideas.

Make sure that your postgresql.conf "listen_addresses" is properly set, also
make sure you dont have a firewall setting that is blocking connections, the
default firewall settings on FC boxes are notorious for blocking pg.

--
Robert Treat
Build A Brighter LAMP :: Beginning PHP and PostgreSQL 8 (ISBN 1590595475)

#5Clodoaldo Pinto
clodoaldo.pinto@gmail.com
In reply to: Robert Treat (#4)
Re: php can't connect to postgresql server

2006/7/3, Robert Treat <xzilla@users.sourceforge.net>:

On Monday 03 July 2006 09:25, Clodoaldo Pinto wrote:

php can't connect to postgresql server

php error log 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\n\tIs the server running on host &quot;127.0.0.1&quot; and
accepting\n\tTCP/IP connections on port 5432?

php connection string:

$conn_string = "hostaddr=127.0.0.1 port=5432 dbname=dbname
user=username password=password";

Also tried host=localhost

It needs to be host, not hostaddr.

Changed to host=localhost as I had already done before with the same
results. From the php manual:

"The currently recognized parameter keywords are: host, hostaddr,..."

What else should I check? I am out of ideas.

Make sure that your postgresql.conf "listen_addresses" is properly set,

What do you mean by properly set? What I had already post is not properly set?:

listen_addresses = '127.0.0.1,xx.xx.xxx.xx'

make sure you dont have a firewall setting that is blocking connections, the
default firewall settings on FC boxes are notorious for blocking pg.

Opened the 5432 port in the firewall and still the same problem.

Clodoaldo

#6Clodoaldo Pinto
clodoaldo.pinto@gmail.com
In reply to: Clodoaldo Pinto (#1)
Re: php can't connect to postgresql server

2006/7/3, Clodoaldo Pinto <clodoaldo.pinto@gmail.com>:

php can't connect to postgresql server

php error log 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\n\tIs the server running on host &quot;127.0.0.1&quot; and
accepting\n\tTCP/IP connections on port 5432?

php connection string:

$conn_string = "hostaddr=127.0.0.1 port=5432 dbname=dbname
user=username password=password";

Also tried host=localhost

pg_hba:

host dbname username 127.0.0.1/32 md5

I can connect as that user with psql:

$ psql -h localhost -U username dbname
Password for user username:
Welcome to psql 8.1.4, the PostgreSQL interactive terminal.

This same setup works in another server running 8.0. Differences:

old server | new server
FC3 | FC5
php 4.3.11 | php 5.1.4
httpd 2.0 | httpd 2.2

What else should I check? I am out of ideas.

The same script running from another server can connect to the new
server using this pg_hba entry:
host dbname usename xx.xx.xxx.xx/32 md5

Clodoaldo

In reply to: Clodoaldo Pinto (#6)
RES: php can't connect to postgresql server

Clodoaldo Pinto worte:

-->-----Mensagem original-----
-->De: pgsql-general-owner@postgresql.org
-->[mailto:pgsql-general-owner@postgresql.org] Em nome de
-->Clodoaldo Pinto
-->Enviada em: segunda-feira, 3 de julho de 2006 12:13
-->Para: pgsql-general postgresql.org
-->Assunto: Re: [GENERAL] php can't connect to postgresql server
-->
-->
-->2006/7/3, Clodoaldo Pinto <clodoaldo.pinto@gmail.com>:
-->> php can't connect to postgresql server
-->>
-->> php error log 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\n\tIs the server running on host &quot;127.0.0.1&quot; and
-->> accepting\n\tTCP/IP connections on port 5432?
-->>
-->> php connection string:
-->>
-->> $conn_string = "hostaddr=127.0.0.1 port=5432 dbname=dbname
-->> user=username password=password";
-->>
-->> Also tried host=localhost
-->>
-->> pg_hba:
-->>
-->> host dbname username 127.0.0.1/32 md5
-->>
-->> I can connect as that user with psql:
-->>
-->> $ psql -h localhost -U username dbname
-->> Password for user username:
-->> Welcome to psql 8.1.4, the PostgreSQL interactive terminal.
-->>
-->> This same setup works in another server running 8.0. Differences:
-->>
-->> old server | new server
-->> FC3 | FC5
-->> php 4.3.11 | php 5.1.4
-->> httpd 2.0 | httpd 2.2
-->>
-->> What else should I check? I am out of ideas.
-->
-->The same script running from another server can connect to
-->the new server using this pg_hba entry:
-->host dbname usename xx.xx.xxx.xx/32 md5
-->
-->Clodoaldo

postgresql.conf -->

listen_addresses = '*' ---- >This is any ip.

pg_hba.conf -->

# TYPE DATABASE USER CIDR-ADDRESS METHOD

# IPv4 local connections:
host all all 127.0.0.1/32 md5

This is the configuration that i choose when install postgresql in windows
xp.

Try this.

Alejandro Michelin Salomon
Porto Alegre
Brasil

#8Clodoaldo Pinto
clodoaldo.pinto@gmail.com
In reply to: Clodoaldo Pinto (#1)
Re: php can't connect to postgresql server

2006/7/3, Clodoaldo Pinto <clodoaldo.pinto@gmail.com>:

2006/7/3, Clodoaldo Pinto <clodoaldo.pinto@gmail.com>:

php can't connect to postgresql server

php error log 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\n\tIs the server running on host &quot;127.0.0.1&quot; and
accepting\n\tTCP/IP connections on port 5432?

php connection string:

$conn_string = "hostaddr=127.0.0.1 port=5432 dbname=dbname
user=username password=password";

Also tried host=localhost

pg_hba:

host dbname username 127.0.0.1/32 md5

I can connect as that user with psql:

$ psql -h localhost -U username dbname
Password for user username:
Welcome to psql 8.1.4, the PostgreSQL interactive terminal.

This same setup works in another server running 8.0. Differences:

old server | new server
FC3 | FC5
php 4.3.11 | php 5.1.4
httpd 2.0 | httpd 2.2

What else should I check? I am out of ideas.

Solved. It was a SELinux problem. From /var/log/messages:

kernel: audit(1151945653.900:39): avc: denied { name_connect } for
pid=17167 comm="httpd" dest=5432 scontext=user_u:system_r:httpd_t:s0
tcontext=system_u:object_r:postgresql_port_t:s0 tclass=tcp_socket

Again followed this:

http://fedora.redhat.com/docs/selinux-faq-fc5/#id2961385

I searched for this problem and I wonder how could nobody reported it
before. Am I the only one connecting locally to pgsql from php with
SELinux enabled in a FC5 box or what?

A much simpler solution:

# setsebool -P httpd_can_network_connect_db 1

Regards, Clodoaldo Pinto