Connecting PHP to PostgreSQL via Unix sockets

I’ve searched the web looking for a way to connect a PHP application to PostgreSQL by Unix sockets using the pg_connect() function, but I didn’t find an answer to my situation.

Today using the trial and error method, I found a solution. There’s two ways to do the connection, since you have Unix socket connection enabled in your server:

1) Using the socket path:

$conn = pg_connect('host=/var/run/postgresql user=[...] bdname=[...]');

2) Omitting the host name/path:

$conn = pg_connect('user=[...] bdname=[...]');

Note: In this case (omitting the host name) the default socket path will be used, probrably that set on postgres.conf file.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.