Returns the underlying PostgreSQL connection socket as a stream resource. This guide demonstrates its use in PHP code and explains when the result or side effect needs extra checks.
What It Does
Returns the underlying PostgreSQL connection socket as a stream resource.
Syntax and Parameters
The examples below show a real call to pg_socket with suitable arguments. Read the function call in context: its connection, handle, input data or other prerequisites are created before use.
Requirements: PostgreSQL extension and a reachable PostgreSQL server.
Example 1: Core Behavior
The returned stream belongs to the PostgreSQL connection; do not close or reconfigure it independently.
<?php
$conn = pg_connect(getenv('PG_DSN') ?: '');
if ($conn === false) throw new RuntimeException('PostgreSQL connection failed');
$stream = pg_socket($conn);
echo is_resource($stream) ? 'Socket stream available' : 'Socket unavailable';
Observe the returned value or changed resource state. A successful function call can still require additional checks before a higher-level operation is considered successful.
Example 2: Alternative or Failure Case
The returned stream belongs to the PostgreSQL connection; do not close or reconfigure it independently.
<?php
$conn = pg_connect(getenv('PG_DSN') ?: '');
if ($conn === false) throw new RuntimeException('PostgreSQL connection failed');
$stream = pg_socket($conn);
echo get_resource_type($stream);
Compare the different inputs, environment assumptions or result checks. Do not use a transport-level success value as proof that all business-level requirements were met.
Practical Example: Applying the Rule
The returned stream belongs to the PostgreSQL connection; do not close or reconfigure it independently.
<?php
$conn = pg_connect(getenv('PG_DSN') ?: '');
if ($conn === false) throw new RuntimeException('PostgreSQL connection failed');
$stream = pg_socket($conn);
echo stream_get_meta_data($stream)["stream_type"] ?? "unknown";
In an application, convert low-level failures into controlled responses and log only non-sensitive diagnostic context.
Important Behavior and Edge Cases
Requires pgsql extension and active PostgreSQL connection; the stream must not be managed independently.
Comparison with Related Functions or Patterns
| Approach | When it differs |
|---|---|
| pg_socket | Returns the underlying PostgreSQL connection socket as a stream resource. |
| Related API or pattern | pg_connection_status inspects connection status without exposing a socket. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Closing or reconfiguring a socket owned by the PostgreSQL connection | Requires pgsql extension and active PostgreSQL connection; the stream must not be managed independently. |
| Substituting a related API without checking its semantics | pg_connection_status inspects connection status without exposing a socket. |
Key Takeaways
- Returns the underlying PostgreSQL connection socket as a stream resource.
- Requires pgsql extension and active PostgreSQL connection; the stream must not be managed independently.
- pg_connection_status inspects connection status without exposing a socket.
Select this API or pattern based on its documented behavior and the trust boundaries in your own application, and test all external integrations against their actual deployments.