How to connect a PostgreSQL database server using PHP PDO?

Razet · · 7709 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:

PHP OPCache: The Secret Weapon for Laravel Performance Boost

OPCache, a built-in PHP opcode cache, is a powerful tool for significantly improving Laravel application speed. This guide will demonstrate how to effectively utilize OPCache to (...)
Harish Kumar

PHP Security Guide: Strategies for Safe and Secure Code

PHP is one of the most widely used server-side scripting languages for web development, powering millions of websites and applications. Its popularity is largely due to its ease (...)
Harish Kumar

How to Use DTOs for Cleaner Code in Laravel, Best Practices and Implementation Guide

When developing APIs in Laravel, ensuring your responses are clear, concise, and consistent is crucial for creating a maintainable and scalable application. One effective way to (...)
Harish Kumar

Data Transfer Objects (DTOs) in PHP: Streamlining Data Flow and Enhancing Code Clarity

Data Transfer Objects (DTOs) are simple objects used to transfer data between software application subsystems. They help in encapsulating data and reducing the number of method (...)
Harish Kumar

PHP Generators: Efficient Data Handling and Iteration Techniques

PHP Generators offer a powerful and memory-efficient way to handle large datasets and complex iteration scenarios in your applications. They provide a more elegant solution compared (...)
Harish Kumar

Compress and Download Files in Laravel Using ZipArchive with Examples

In web development, file compression is essential for optimizing data transfer and storage. Laravel provides tools for creating and downloading compressed files. This guide explores (...)
Harish Kumar