PHP Connection to PostgreSQL
PHP Connection to PostgreSQL
This guide shows how to connect a PHP application hosted on the platform to a PostgreSQL database. The workflow includes creating the environment, enabling the PostgreSQL PHP extension, configuring the connection string, checking connectivity, and running a simple database query.
Create the Environment
Create a PHP and PostgreSQL environment
Log in to the platform dashboard and create a new environment with a PHP application server, such as Apache PHP, together with a PostgreSQL database.

Check the database credentials
After environment creation, check the email sent by the platform. It contains the PostgreSQL login and password required to access the database.

Once the database is ready, it can be accessed through its web administration panel and connected to the PHP application.
Configure the PHP Server
Open Apache configuration
Click Config for the Apache PHP application server.

Enable the PostgreSQL PHP extension
Navigate to the etc folder, open php.ini, and add:
extension=pgsql.so

Restart Apache nodes
Save the configuration changes and click Restart Nodes for the Apache server layer.

Configure the PostgreSQL Connection
Use the PHP pg_connect() function to open the PostgreSQL connection:
pg_connect("host={host} port={port} dbname={dbname} user={user} password={password}");
http://. Example: node171206-php-postgresql.jelastic.com.5432.webadmin account.Close the PostgreSQL connection with:
pg_close();
PHP pages that use the database
The required connection functions must be included in every PHP page that needs PostgreSQL access, either directly or through a shared include file.
Connection Check Up
Use a simple PHP page to test the PostgreSQL connection:
<?php
$dbconn = pg_connect(
"host=postgres.jelastic.com port=5432 dbname=postgres user=webadmin password=passw0rd"
);
if (!$dbconn) {
echo "<center><h1>Doesn't work =(</h1></center>";
} else {
echo "<center><h1>Good connection</h1></center>";
}
pg_close($dbconn);
?>
Replace the sample values with the actual PostgreSQL hostname, database name, username, and password before testing.
Execute a Simple PostgreSQL Query
The following example connects to PostgreSQL, runs a query against test_table, and displays the first two values from each returned row:
<?php
$conn = pg_connect(
"host=postgres.jelastic.com port=5432 dbname=postgres user=webadmin password=passw0rd"
);
if (!$conn) {
echo "An error occurred.\n";
exit;
}
$result = pg_query($conn, "SELECT * FROM test_table");
if (!$result) {
echo "An error occurred.\n";
exit;
}
while ($row = pg_fetch_row($result)) {
echo "value1: $row[0] value2: $row[1]";
echo "<br />\n";
}
?>
These connection and query examples can be adapted for a PHP application that uses PostgreSQL as its database.
Expected Result
The Apache PHP server has PostgreSQL support enabled through pgsql.so. The PHP application can establish a connection using pg_connect(), execute PostgreSQL queries with pg_query(), process rows with pg_fetch_row(), and close the connection with pg_close().
Important Notes
- Enable
extension=pgsql.soinphp.inibefore using PostgreSQL functions. - Restart the Apache PHP nodes after changing
php.ini. - Use the PostgreSQL host without
http://orhttps://. - The documented default PostgreSQL port is
5432. - The source example uses
webadminas the database user. - Replace all sample hostnames and credentials before using the example code.
