Content · Web Development · PHP

PHP pg_connection_status() Function: Usage and Examples

Returns the current connection status constant. A connected handle may still have a failed SQL statement or aborted

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Returns the current connection status constant.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Returns the current connection status constant.

Syntax and Return Value


pg_connection_status(...)

The return value and exceptional cases depend on the call described here: A connected handle may still have a failed SQL statement or aborted transaction.

Requirements: install and enable the corresponding PHP driver or extension and configure the service before running the examples.

Example 1: Basic Use


Requires a reachable PostgreSQL server and the pgsql PHP extension; asynchronous requests must be drained with pg_get_result.

<?php
$host = getenv('DB_HOST'); $user = getenv('DB_USER'); $password = getenv('DB_PASSWORD'); $database = getenv('DB_NAME');
if (!$host || !$user || $password === false || !$database) throw new RuntimeException('Missing PostgreSQL configuration');
$pg = pg_connect('host=' . $host . ' dbname=' . $database . ' user=' . $user . ' password=' . $password);
if (!$pg) { throw new RuntimeException('PostgreSQL connection failed'); }
echo pg_connection_status($pg) === PGSQL_CONNECTION_OK ? "connected" : "not connected";

Example 2: A Second Scenario


Requires a reachable PostgreSQL server and the pgsql PHP extension; asynchronous requests must be drained with pg_get_result. This additional check distinguishes the method's result from connection or statement state.

<?php
$host = getenv('DB_HOST'); $user = getenv('DB_USER'); $password = getenv('DB_PASSWORD'); $database = getenv('DB_NAME');
if (!$host || !$user || $password === false || !$database) throw new RuntimeException('Missing PostgreSQL configuration');
$pg = pg_connect('host=' . $host . ' dbname=' . $database . ' user=' . $user . ' password=' . $password);
if (!$pg) { throw new RuntimeException('PostgreSQL connection failed'); }
echo pg_connection_status($pg) === PGSQL_CONNECTION_OK ? "connected" : "not connected";
echo pg_connection_status($pg) === PGSQL_CONNECTION_BAD ? "broken connection" : "connection reported OK";

Example 3: Practical Checks


Requires a reachable PostgreSQL server and the pgsql PHP extension; asynchronous requests must be drained with pg_get_result. This additional check distinguishes the method's result from connection or statement state.

<?php
$host = getenv('DB_HOST'); $user = getenv('DB_USER'); $password = getenv('DB_PASSWORD'); $database = getenv('DB_NAME');
if (!$host || !$user || $password === false || !$database) throw new RuntimeException('Missing PostgreSQL configuration');
$pg = pg_connect('host=' . $host . ' dbname=' . $database . ' user=' . $user . ' password=' . $password);
if (!$pg) { throw new RuntimeException('PostgreSQL connection failed'); }
echo pg_connection_status($pg) === PGSQL_CONNECTION_OK ? "connected" : "not connected";
echo "Transport status code: " . pg_connection_status($pg);

Important Behavior and Edge Cases


A connected handle may still have a failed SQL statement or aborted transaction.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
pg_connection_statusReturns the current connection status constant.
Related API / patternpg_transaction_status reports transaction state, which is a different concern.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesA connected handle may still have a failed SQL statement or aborted transaction.
Using a related API interchangeablypg_transaction_status reports transaction state, which is a different concern.

Key Takeaways


  • Returns the current connection status constant.
  • A connected handle may still have a failed SQL statement or aborted transaction.
  • pg_transaction_status reports transaction state, which is a different concern.

Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.

Share this resourceSend this page to someone who may find it useful.