08001
Read PG 18 manual ↗sqlclient_unable_to_establish_sqlconnection
- SQLSTATE
- 08001
- Condition name
- sqlclient_unable_to_establish_sqlconnection
- Class
- Connection Exception
- Source macro
- ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION
- Evidence
- Observed at runtime in the source evidence
English SQLSTATE atlas: authored explanations and source/runtime evidence are separate from the manual definitions. View source ↗
At a glance
SQLSTATE 08001 is sqlclient_unable_to_establish_sqlconnection in Class 08. 08001 is the server-side SQLSTATE for a client connection that could not be established. In the selected dblink_connect path the ERROR primary is could not establish connection and the refused-port reason is dynamic DETAIL; this differs from 08003 (an absent dblink handle) and startup failures such as 28000, 28P01, and 3D000.
Meaning
The server-side dblink_connect branch emits this code after libpq cannot open the requested remote endpoint. Its fixed diagnostic is an ERROR with primary could not establish connection; the endpoint and operating-system reason are dynamic DETAIL text. The condition concerns establishing a named remote handle, so it is different from an absent handle (08003) and from startup rejection before a session exists (28000 or 3D000).
The source does not format the DETAIL itself: dblink.c passes the libpq error string through errdetail_internal("%s", msg). That distinction matters when comparing versions or clients. The 18.6 refusal observed below includes the host, port, and Connection refused text, while the 10.21 refusal uses a different libpq wording; neither wording is a fixed 08001 template.
Diagnosis
Record the target host, port, authentication parameters, and complete DETAIL. The selected 18.6/10.21 runs leave the local autocommit session IDLE after the refused attempt; a real dblink handle then connects to the runner-owned target, runs remote SELECT 1, and is explicitly disconnected. That proves local recovery and handle cleanup, not completion of any remote business transaction.
Read the failure stage before retrying. A refused TCP endpoint, DNS or TLS failure, and authentication rejection can all be returned by libpq through this dblink branch, so inspect the dynamic DETAIL rather than classifying from the code alone. If the call runs inside an explicit local transaction, the ERROR can leave that transaction unusable until ROLLBACK (or ROLLBACK TO SAVEPOINT); the autocommit IDLE result in the selected case does not imply that an explicit transaction is still usable. The dblink handle belongs to the backend that created it, so check and repair it on that same session or pool member.
Response
Correct the endpoint or connection parameters, open a new handle, and verify a harmless remote probe before sending business work. If the failed request could have crossed a remote boundary, reconcile it before retrying; 08001 alone does not justify replaying a non-idempotent operation.
In autocommit, retrying the connection attempt after correcting the endpoint is a new statement and the owner session can remain usable. In an explicit transaction, first recover the local transaction, then establish and probe a fresh handle; a savepoint is useful only when the surrounding work is deliberately designed to continue. If the remote operation may have reached the target before the local error was reported, reconcile its result before replaying it.
Observed diagnostics
The fixed dblink_connect branch emits ERROR with primary could not establish connection and passes a dynamic libpq string as DETAIL (errdetail_internal("%s", msg)). In the selected 18.6 run that value was connection to server at "127.0.0.1", port 1 failed: Connection refused followed by libpq's hint; in the selected 10.21 run it began could not connect to server: Connection refused. Those are run-specific values, not a fixed SQLSTATE message template. Other producers may choose different text; a client-side exception without the server diagnostic is not evidence for this SQLSTATE.
The severity is fixed at ERROR for this dblink path. The selected local session stayed IDLE because the case used autocommit; do not transfer that status to a surrounding explicit transaction or to a remote transaction whose outcome was not observed.
Representative case
The SQL block creates dblink, attempts a refused runner-local port, then probes the owner session.
runner_host, runner_port, runner_db, and runner_user are runner placeholders, not literal values for a manual copy. Replace them with a reachable target and a login role that may connect to it; installing/using dblink and the remote login require the corresponding privileges. Keep the statements on the same owner backend and use the case's autocommit boundary if you want the selected recovery observation. The trigger is a server-side dblink attempt; it does not execute work on a remote transaction.
CREATE EXTENSION IF NOT EXISTS dblink;
SELECT dblink_connect('missing_remote', 'host=127.0.0.1 port=1 dbname=postgres connect_timeout=1');
SELECT dblink_connect('working_remote', 'host=runner_host port=runner_port dbname=runner_db user=runner_user connect_timeout=5');
SELECT * FROM dblink('working_remote', 'SELECT 1') AS result(value integer);
SELECT dblink_disconnect('working_remote');
SELECT 1;
The selected 18.6 run reports SQLSTATE 08001, primary could not establish connection, and leaves the owner session IDLE after the failed operation. The controlled repair opened the named handle with OK, returned remote 1, and disconnected it with OK; the final owner probe returned 1 and IDLE. The 10.21 selected run passed the same assertions.
The downloadable case and evidence projections are 08001 case JSON and authored evidence. The runner manifest is verify/cases/08001/cases.json; the page SQL is checked against its shared registry before publication.
Versions
The generated facts table records the locked catalogue snapshots and earliest observed definition. The selected natural runtime scope is PostgreSQL 18.6 and 10.21; this does not infer behavior for every intermediate release.
Sources
src.dblink-connect.18.6—contrib/dblink/dblink.catREL_18_6commit724edf9bde9d356724ad384a2e196edc3c9f80f7; fixed blob SHA-256e4cfaec3a0a1e5d23fded584725e0f6cf7a17321ad99e5fe046e8b7f97416f15(source).src.dblink-connect.10.23—contrib/dblink/dblink.catREL_10_23commit02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4; fixed blob SHA-2562d542cc722ba361bd59990aaed7fe7c0f8e147155df5e5fcd56ee03b4dd27c61(source).src.calls.REL_18_6/src.calls.REL_10_23— fixed local call scans, SHA-2569ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf/00d16d3eb01b71ccf1b245c8f3102f9d0ec9f36fb02777b8dd1b99fcb263040c; these scans preserve the resolved call context used by the claims.
Source evidence
Evidence belongs to the frozen source and runtime versions listed here. It is not a runtime verification of the selected manual version.
08001 is the sqlclient_unable_to_establish_sqlconnection condition in Class 08.
Method: Read the fixed errcodes.txt definition and the locked catalogue metadata.
Limits: Directory identity does not identify every backend or client path.
The fixed source paths associated with 08001 report the condition in the mechanism selected for this page.
Method: Trace the resolved source call records at the fixed release commits and compare their dynamic message fields.
Limits: Other calls can retain the same SQLSTATE with different context or text.
The selected dblink_connect_failure case passed with the expected structured diagnostics, recovery assertions, and cleanup on PostgreSQL 18.6 and 10.21.
Method: Run the shared registry case on isolated runner-owned latest and PG10 targets; inspect the final summaries and raw results.
Limits: This covers the selected case and versions only; it does not generalize to every driver, proxy, or intermediate release.
The selected dblink ERROR is observed under autocommit, where the owner session remains IDLE; when the same ERROR occurs inside an explicit transaction, PostgreSQL's transaction recovery rules require rollback or an intentional savepoint before unrelated work.
Method: Combine the fixed dblink ERROR source path with the official transaction and savepoint recovery contract.
Limits: No explicit-transaction dblink runtime was selected in this batch; the transaction guidance is the general ERROR boundary, not a claim about a specific remote topology.
The locked catalogue records 08001 in the listed snapshots; the runtime comparison here is limited to PostgreSQL 18.6 and 10.21.
Method: Read the generated facts block and locked manifest, then compare the selected target summaries.
Limits: Presence in a definition file is not an exact behavioral introduction; the two runtime targets do not prove all middle versions.
- src/backend/utils/errcodes.txt · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 raw/calls/REL_18_6.jsonl· REL_18_6 ·raw/calls/REL_10_23.jsonl· REL_10_23 ·- contrib/dblink/dblink.c · REL_18_6 ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - contrib/dblink/dblink.c · REL_10_23 ·
02991e79f8f58bc208f05dcc8af0c62dbe0a6ea4 - doc/src/sgml/xact.sgml · PG18-docs ·
724edf9bde9d356724ad384a2e196edc3c9f80f7 - verify/cases/08001/cases.json · workspace ·
- verify/cases/08001/snippets.json · workspace ·
Message templates
explicit ERROR · message.dblink-establish
Primary
could not establish connection
Detail
%s
Reproduction & repair cases
dblink_connect_failure · PG 10, 18
Preconditions
- A runner-owned disposable target is provisioned.
Trigger: Connect to a runner-local refused port through server-side dblink.
Expected assertions
- The selected server diagnostic has the expected SQLSTATE
- The selected recovery/probe assertions pass
- Runner-owned resources are cleaned up
Repair: Follow the case-specific repair statements and verify the resulting state.
Cleanup: Drop the case schema with an owner connection.
Recorded runtime evidence
18.6 (Homebrew) · passed
Run: 08001-dblink-repair-latest2
Disposable runner-owned target only; this is a bounded mechanism case, not a guarantee for every client or network path.
{
"probe": 1,
"statuses": {
"final": "IDLE",
"after_error": "IDLE"
},
"diagnostic": {
"text": "could not establish connection\nDETAIL: connection to server at \"127.0.0.1\", port 1 failed: Connection refused\n\tIs the server running on that host and accepting TCP/IP connections?",
"context": null,
"severity": "ERROR",
"sqlstate": "08001",
"table_name": null,
"column_name": null,
"schema_name": null,
"source_file": "dblink.c",
"source_line": "338",
"message_hint": null,
"datatype_name": null,
"exception_type": "SqlclientUnableToEstablishSqlconnection",
"internal_query": null,
"message_detail": "connection to server at \"127.0.0.1\", port 1 failed: Connection refused\n\tIs the server running on that host and accepting TCP/IP connections?",
"constraint_name": null,
"message_primary": "could not establish connection",
"source_function": "dblink_connect",
"internal_position": null,
"statement_position": null,
"severity_nonlocalized": "ERROR"
},
"dblink_repair": {
"opened": "OK",
"disconnected": "OK",
"remote_probe": 1
},
"connection_scope": "server-side dblink connection to the same runner-owned target and a refused runner-local port",
"connection_autocommit": true
}10.21 (Debian 10.21-1.pgdg90+1) · passed
Run: 08001-dblink-repair-pg10c
Disposable runner-owned target only; this is a bounded mechanism case, not a guarantee for every client or network path.
{
"probe": 1,
"statuses": {
"final": "IDLE",
"after_error": "IDLE"
},
"diagnostic": {
"text": "could not establish connection\nDETAIL: could not connect to server: Connection refused\n\tIs the server running on host \"127.0.0.1\" and accepting\n\tTCP/IP connections on port 1?",
"context": null,
"severity": "ERROR",
"sqlstate": "08001",
"table_name": null,
"column_name": null,
"schema_name": null,
"source_file": "dblink.c",
"source_line": "302",
"message_hint": null,
"datatype_name": null,
"exception_type": "SqlclientUnableToEstablishSqlconnection",
"internal_query": null,
"message_detail": "could not connect to server: Connection refused\n\tIs the server running on host \"127.0.0.1\" and accepting\n\tTCP/IP connections on port 1?",
"constraint_name": null,
"message_primary": "could not establish connection",
"source_function": "dblink_connect",
"internal_position": null,
"statement_position": null,
"severity_nonlocalized": "ERROR"
},
"dblink_repair": {
"opened": "OK",
"disconnected": "OK",
"remote_probe": 1
},
"connection_scope": "server-side dblink connection to the same runner-owned target and a refused runner-local port",
"connection_autocommit": true
}Definition snapshot: english-manuals:272c51e8f604747c40be95a991b… · English manual source