PostgreSQL in cPanel: creating a database and connecting from PHP

On shared hosting PostgreSQL 13.23 is active and has tools in cPanel. Create the database and user under PostgreSQL Databases, link one to the other and, to talk to it from PHP, switch on the pgsql module in “Select PHP Version”. The module exists but comes switched off by default, and that is why the error Call to undefined function pg_connect() appears.

Step by step

1 In cPanel open PostgreSQL Databases. Under create new database, type a name. cPanel adds the account prefix, as with MySQL databases.
2 In the users section, create a user with a strong password (the password generator will do).
3 In the section that links a user to a database, join the two and choose the privileges (for an application, all of them, on that database).
4 Open Select PHP Version, pick the PHP version of the site’s folder and tick the pgsql extension. If the list also has pdo_pgsql, tick it to use PDO. See switching extensions on and off.
5 Create a test file in the site folder:<?php
$conn = pg_connect('host=localhost port=5432 dbname=account_database user=account_user password=your-password-here');
echo $conn ? 'Connected' : 'Failed';
6 Open the file in the browser, check for Connected and delete the file.
Detail Value
Server (host) localhost
Port 5432
Database and user name With the account prefix, written in full.
Connecting from outside No. Port 5432 is not open to the internet. Use an SSH tunnel.
If pg_connect still says “undefined function”, the PHP version chosen in “Select PHP Version” is not the one for the folder the file is in. The version is chosen per folder. Check which one it is and whether the extension was saved. See viewing and adjusting your PHP configuration.

Using PostgreSQL with an application

Many applications have an install screen that asks for the database type. Choose PostgreSQL, with host localhost and port 5432, and use the full database and user names. Leave the database empty: the installer creates the tables. If a schema is requested, the usual answer is public. One point to note: the PHP pgsql module only matters for PHP applications. Node and Python do not need it, because they use their own libraries. See connecting from Python and Node.js. If the install fails saying it cannot find the database, the most common cause is the name written without the account prefix.

To connect from home with a client such as DBeaver or psql, use an SSH tunnel. See the SSH tunnel, step by step. And if your application accepts MySQL or PostgreSQL, see which to choose.

Created the database and the application cannot find it? Tell us the folder’s PHP version and the exact error and we will look.

Open a support ticket

SEE ALSO

Switching a PHP extension on or off in Select PHP Version

Remote access to PostgreSQL: what works and what does not

PostgreSQL or MySQL: which one for your project

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from 321,75 MT/mo (3-year plan, with coupon)

See plans
  • 0 Users Found This Useful
Was this answer helpful?