This article describes two methods for connecting to a PostgreSQL database using PHP:
- PostgreSQL native functions
- PDO [PHP Data Objects]
The PostgreSQL databases and users must already exist before you can use these methods. For information about how to manage PostgreSQL databases using cPanel, please see this article.
Connecting to PostgreSQL using native functions
PHP provides many functions for working directly with PostgreSQL databases.
To connect to PostgreSQL using native functions, follow these steps:
- Use the following PHP code to connect to PostgreSQL and select a database. Replace username with your username, password with your password, and dbname with the database name:
After the code connects to PostgreSQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the employees table, and stores the result in the $result variable:
Connecting to PostgreSQL using PDO [PHP Data Objects]
The PostgreSQL functions in the previous procedure can only be used with PostgreSQL databases. PDO abstracts database access, and enables you to use code that can handle different types of databases.
To connect to PostgreSQL using PDO, follow these steps:
- Use the following PHP code to connect to PostgreSQL and select a database. Replace username with your username, password with your password, and dbname with the database name:
After the code connects to PostgreSQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the employees table, and stores the result in the $result variable:
More Information
- For more information about PostgreSQL functions in PHP, please visit //php.net/manual/en/book.pgsql.php.
- For more information about PDO, please visit //www.php.net/manual/en/book.pdo.php.
SUMMARY: This article reviews the steps necessary for connecting to a PostgreSQL database using PHP.
1. Installing PHP
2. Connecting to the PostgreSQL from PHP
a. PHP command line interface
b. PHP API
Note: although the steps in this post refer to the EnterpriseDB Advanced server, the same steps work for PostgreSQL community version as well.
Installing PHP
Before you can begin developing PHP scripts that interact with a PostgreSQL database, first you need to confirm if PHP is installed on your machine, and second you will need to confirm PostgreSQL support in your PHP installation.
To confirm if PHP is installed on your Linux machine, you can use the following command:
[root@kubemaster ~]# rpm -qa | grep php
Php-json-7.3.11-1.el7.remi.x86_64
php-opcache-7.3.11-1.el7.remi.x86_64
php-common-7.3.11-1.el7.remi.x86_64
php-gd-7.3.11-1.el7.remi.x86_64
php-pdo-7.3.11-1.el7.remi.x86_64
php-mysqlnd-7.3.11-1.el7.remi.x86_64
php-pecl-mcrypt-1.0.3-1.el7.remi.7.3.x86_64
php-7.3.11-1.el7.remi.x86_64
Php-cli-7.3.11-1.el7.remi.x86_64
If PHP is not installed, then you can refer to the PHP installation instructions available here: //www.linuxtechi.com/install-php-7-centos-7-rhel-7-server/
If you’re not sure whether your existing PHP installation already has PostgreSQL support, create a simple script named phpinfo.php [which should be placed in your web server’s document root, i.e.,/var/www/html/], containing the following line:
Check the output of this script in your web browser. If PostgreSQL support has already been included, the output will contain a section similar to the following:
Note: In this document “web server” refers to an Apache web server whose document root is /var/www/html.
Connecting to the PostgreSQL server from PHP
All interactions with the PostgreSQL database are performed through the PostgreSQL extension, which is a comprehensive set of PHP functions. For a complete list of functions and information about what they do, refer to the PHP Manual instructions: //www.php.net/manual/ref.pgsql.php.
There are two ways we can connect to the PostgreSQL database:
- Using the PHP command line interface.
- Using PHP API.
PHP command line Interface
Using the functions below we can connect to the PostgreSQL database:
[root@localhost bin]#cd /usr/bin/
[root@localhost bin]# ./php -a
Interactive shell:
php > pg_connect["host=localhost dbname=edb user=enterprisedb password=postgres"];
php > pg_query["create table test[id integer]"];
php > exit
Output from the database:
edb=# \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+--------------
public | test | table | enterprisedb
[1 row]
edb=# \d+ test
Table "public.test"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
--------+---------+-----------+----------+---------+---------+--------------+-------------
id | integer | | | | plain | |
PHP API
A simple PHP script that opens a connection to a PostgreSQL database, create a table would look something like this:
[root@localhost bin]#cd /var/www/html
[root@localhost bin]#cd /var/www/html
[root@localhost bin]#vi test.php
The above test.php file needs to be executed from the browser at //localhost/test.php:
As you can see, interacting with the database from within PHP is fairly straightforward using pg_connect[]. If the connection attempt fails, the pg_connect[] function will return false. Failed connection attempts can, thus, be detected by testing the return value:
[root@localhost bin]#cd /var/www/html
[root@localhost bin]#vi connection.php
Execute the connection.php script from the browser at //localhost/connection.php:
The PHP script below is a single script you can use to perform PHP connection testing, query status checking, and data fetching from the database.
========================================
[root@localhost bin]#cd /var/www/html
[root@localhost bin]#vi connection_test.php
Execute the connection_test.php script from the browser at //localhost/connection_test.php: