Fatal error: Call to undefined function pg_connect()

Fatal error: Call to undefined function pg_connect()

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Fatal error: Call to undefined function pg_connect()

When PHP throws a "Fatal error: Call to undefined function pg_connect()", it usually signals one of two distinct states: the PostgreSQL extension isn't loaded, or you are inadvertently using the procedural interface in an environment that only supports the object-oriented interface. Based on the symptoms described—where phpinfo() confirms the module is enabled yet the function remains undefined—the issue almost certainly lies with how the extension is being loaded versus how the code is invoking it.

The error message explicitly states that pg_connect is undefined. This function belongs to the procedural API of the PostgreSQL driver. However, modern PHP installations often default to or prioritize the Object-Oriented (OO) interface when multiple versions or conflicting configurations exist, especially if the extension file name suggests a specific version mismatch.

The Configuration Mismatch

The most common cause for this specific discrepancy is a conflict between the procedural and object-oriented interfaces within the same php.ini or configuration directory. If you see "PostgreSQL Support" enabled in phpinfo(), the C extension (pgsql.so) is active. However, if the configuration also loads the PDO PostgreSQL driver (pdo_pgsql), PHP sometimes exposes a different set of functions depending on which module takes precedence during initialization.

In many legacy setups or specific hosting environments, simply uncommenting extension=pgsql.so isn't enough if there is a conflicting directive elsewhere, such as an older version of the extension loaded before the new one, or if the procedural interface was explicitly disabled in favor of PDO.

To resolve this without rewriting your application logic, you can switch to the Object-Oriented API. This approach is more robust and aligns with modern PHP best practices. Instead of pg_connect(), use new PgSqlConnection() (or PgSql\Connection depending on the PHP version).

Here is how you can refactor the connection string from your example:

// Old procedural style (causing undefined function error)
$conn_string = "host=$hostname port=5432 dbname=$dbname user=$db_user password=$db_password";
$db = pg_connect($conn_string); 

// Modern Object-Oriented style
$dsn = "pgsql:host=$hostname;port=5432;dbname=$dbname";
$options = [
    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];

try {
    $db = new PDO("mysql:$dsn", $db_user, $db_password); // Note: DSN syntax varies slightly for pgsql
} catch (PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}

Correction for PostgreSQL PDO: The DSN for PostgreSQL in PDO is typically constructed as pgsql:host=.... Ensure you are passing the username and password correctly to the constructor.

Verifying the Extension Load

If you must stick to procedural functions, you need to ensure that the specific procedural library is loaded before any other conflicting modules. Check your php.ini file (or the runtime configuration files in /etc/php/.../mods-available/ on Linux). Look for lines like:

extension=pgsql.so
; extension=pdo_pgsql.so ; <-- Ensure this isn't causing a conflict if not needed

If you are on a system where phpinfo() shows the module but the function is missing, it is highly likely that the procedural API was compiled out or disabled via an explicit directive like disable_functions. Run the following command to double-check:

php -m | grep pgsql
# Or check specifically for disabled functions
php -i | grep "disabled functions"

If pg_connect is missing but pgsql appears in the module list, inspect the php.ini for a line disabling specific functions. If no such line exists, it may be that you are running a version of PHP where the procedural interface was deprecated or removed in favor of PDO-only implementations, though this is rare in stable releases.

The "pqsql.so" File Location Issue

The source material mentions copying pqsql.so to the Apache/bin directory. This is incorrect for standard PHP setups. Extension files must reside in the directory designated by the extension_dir directive in your active php.ini. Copying a .so file into the binary directory (bin/) does not affect the PHP runtime; it only affects the system library path or the Apache executable itself, which is not how PHP loads extensions.

To fix this:

  1. Locate the correct extension directory (often /usr/lib/php/20180731/ or similar).
  2. Ensure extension=pgsql.so is uncommented in the php.ini file located by php --ini.
  3. Restart Apache or PHP-FPM immediately after changes.

Why This Happens on Remote vs. Local Servers

The discrepancy between remote and local environments (like MAMP) often arises because the remote server might be running a different PHP version or a different build of the PostgreSQL extension than your local machine. Even if phpinfo() shows "PostgreSQL Support => enabled," the underlying C library version (libpq) must match the expectations of the PHP extension. If the remote server has a newer PostgreSQL version but an older PHP extension, function signatures might differ, leading to undefined errors for procedural calls.

Switching to PDO or the Object-Oriented API is the most reliable solution because it abstracts these low-level C library differences away from your application logic, ensuring compatibility across diverse server environments.

Our network