PDO Connecting to PostgreSQL

Summary: this tutorial shows you how to connect to a PostgreSQL database server using PHP PDO.


Before connecting to the PostgreSQL database server using PHP PDO, you need to have:

  • A PostgreSQL database server, a sample database and account with username and password that can access the database.
  • PHP PDO PostgreSQL driver enabled in your web server.

For example, we have a local PostgreSQL database server that has pagila sample database and an account with postgres username and postgres password which can access the pagila database. We can create a new database configuration file named dbconfig.php and put these database parameters into the file as shown below:

Later on, we will use the dbconfig.php configuration file in other script files that need to access this configuration parameters by using the require_once() function.

To check if the PDO PostgreSQL driver is enabled, you open the php.ini file and check if the following line is un-commented. If it is not, you can remove the semicolon ( ;) in front of the entry.

PostgreSQL data source name

The data source name or DSN conveys the database parameters that allow you to connect to a database system. PDO defines different DSN for different database system. The data source name of the PostgreSQL is composed of the following parameters:

  • DNS prefix: pgsql:
  • host: the database server’s host name where the PostgreSQL database locates.
  • port: the port which PostgreSQL database is running, the default port is 5432.
  • dbname: database name.
  • user: The name of user that connects to the database dbname. You can specify the user name in either DSN or in constructor of the PDO class.
  • password: The password of the user name. You can specify the password in either the DSN or the PDO constructor.

Notice that PDO ignores the username and password in the PDO constructor if you put them in the data source name (DSN). The following is the DSN that allows us to connect to pagila database in the local PostgreSQL database server.

Connecting to PostgreSQL

The following code illustrates how to connect to the pagila database in PostgreSQL database server:

How the script works.

  • To connect to a PostgreSQL database, you just need to create a new connection object, which is an instance of the PDO class. When you create a new connection object, you pass the DSN as an argument to its parameter.
  • The try catch statement is used to catch any exception that may occur during connecting to the PostgreSQL database. In the catch block, we display the error message if there is anything wrong with the connection.

If you have everything setup correctly, you will see the following message:

If you are using WAMP server, after enabling the POD PostgreSQL driver, it is still not working and you got the following error message:

You can put the following line:

At the end of the httpd.conf file in the C:\wamp\bin\apache\apache2.2.22\conf folder.

In this tutorial, we have shown you step by step how to connect to the PostgreSQL database server using PHP PDO.