How to connect a PostgreSQL database server using PHP PDO?

Razet · · 7217 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.

extension=php_pdo_pgsql.dll

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:

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

Please login or create new account to add your comment.

0 comments
You may also like:

A Comprehensive Guide to #[Override] Attribute in PHP 8.3

PHP 8.3 has ushered in an array of advanced features, among which the  #[Override] attribute stands out. This attribute, while known in other languages, is a fresh addition to (...)
Harish Kumar

Laravel Pint & VS Code: Automate Your Code Formatting

Laravel Pint is an opinionated PHP code style fixer built on top of PHP-CS-Fixer, designed to simplify the process of ensuring clean and consistent code style in Laravel projects. (...)
Harish Kumar

Best Practices for Testing and Quality Assurance in Custom Web App Development

In the fast-paced world of web app development, delivering high-quality products that meet customer expectations is crucial. But how can you ensure your custom web application (...)
VisionX Technologies

PHP-CS-Fixer: The Ultimate Guide to PHP Code Formatting in VSCode

In this comprehensive guide, we will explore how to use PHP-CS-Fixer in VSCode to automate the process of PHP code formatting and adhere to the best coding practices.
Harish Kumar

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