How to connect a PostgreSQL database server using PHP PDO?

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

What's New in PHP 8.4: Key Enhancements and Updates

As PHP 8.4's release on November 21, 2024, approaches, it's clear that PHP continues to evolve and delight its developer community. For those who have been coding with PHP since (...)
Harish Kumar

Introducing Tools to Supercharge PHP-FPM Efficiency and Monitoring

PHP-FPM stands for PHP FastCGI Process Manager. It’s an improved way to manage PHP processes that makes web applications faster and more efficient. Instead of running each PHP (...)
Harish Kumar

PHP 8.4 Property Hooks: The Ultimate Guide for Developers

PHP 8.4, coming in November 2024, introduces a new feature called property hooks. This feature makes it easier to work with class properties by allowing you to define custom behavior (...)
Harish Kumar

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