How to connect a PostgreSQL database server using PHP PDO?

Razet · · 6135 Views

Before connecting with the PostgreSQL database using PHP PDO, make sure PHP PDO PostgreSQL driver enabled.

To check if the PDO PostgreSQL driver is enabled, 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 passes on the database parameters that permit you to connect with the database. PDO characterizes distinctive DSN for different databases. The data source name of the PostgreSQL is made out of the following parameters:

  1. DNS prefix: pgsql:

  2. host: the database server’s hostname where the PostgreSQL database locates.

  3. port: default port is 5432.

  4. dbname: database name.

  5. user: The name of the user that connects to the database dbname.

  6. password: The password of the user name.

Notice that PDO ignores the username and password in the PDO constructor if you put them in the data source name (DSN).

Connecting to PostgreSQL

The following snippet allows you to connect to the database in the PostgreSQL database server:

$db = 'database';
$username = 'user';
$password = 'password';
$dsn = "pgsql:host=$host;port=5432;dbname=$db;user=$username;password=$password";
 // create a PostgreSQL database connection
 $conn = new PDO($dsn);
 // display a message if connected to the PostgreSQL successfully
    echo "Connected to the <strong>$db</strong> database successfully!";
} catch (PDOException $e){
 // report error message
 echo $e->getMessage();

Please login or create new account to add your comment.

You may also like:

Learn PHP Development from beginning.

PHP stance Hypertext Preprocessor, it's server-side scripting laungage and easy to use. PHP  also well  known for its speed, simplicity, flexibility features that have made it (...)
Programmer Desk

What is the difference between classes vs enums in PHP 8.1?

One of the common questions that I have been asked is what is the difference between classes and enums. Class(es) and enum(s) share many similarities but differ in some aspects. (...)
Harish Kumar

How to use the enumerations(Enums) of PHP 8.1 in Laravel?

The release of PHP 8.1 brings native enumerations to PHP. There is no more requirement for custom solutions in your Laravel projects since the Laravel v8.69 release has you back. (...)
Harish Kumar

What is Enumerations(Enums) in PHP 8.1? Enums in PHP v8.1 Explained in detail with examples.

PHP 8.1 added many new features, and Enumerations (Enum) is our favourite new feature. Enumerations (or enums for short) allow us to define a new structure similar to a class however (...)
Harish Kumar

Web App Development Technologies

If you have been planning to create robust websites or web apps, you should be aware of the various technologies required to make it happen. Over time, these technologies have (...)
Narola Infotech

How to Upload Multiple Images with jQuery AJAX and PHP, with preview

Uploading an image without page refresh is more user-friendly than refreshing the entire page. So, in this guide, you will learn how to upload multiple images using ajax in jQuery (...)
Nakul Kumar