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:

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

2) Omitting the host name/path:

<?php
$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.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s