2 .\" Title: dblink_connect
3 .\" Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
6 .\" Manual: PostgreSQL 18.0 Documentation
7 .\" Source: PostgreSQL 18.0
10 .TH "DBLINK_CONNECT" "3" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 dblink_connect \- opens a persistent connection to a remote database
35 dblink_connect(text connstr) returns text
36 dblink_connect(text connname, text connstr) returns text
40 \fBdblink_connect()\fR
41 establishes a connection to a remote
43 database\&. The server and database to be contacted are identified through a standard
45 connection string\&. Optionally, a name can be assigned to the connection\&. Multiple named connections can be open at once, but only one unnamed connection is permitted at a time\&. The connection will persist until closed or until the database session is ended\&.
47 The connection string may also be the name of an existing foreign server\&. It is recommended to use the foreign\-data wrapper
49 when defining the foreign server\&. See the example below, as well as
50 CREATE SERVER (\fBCREATE_SERVER\fR(7))
52 CREATE USER MAPPING (\fBCREATE_USER_MAPPING\fR(7))\&.
57 The name to use for this connection; if omitted, an unnamed connection is opened, replacing any existing unnamed connection\&.
62 libpq\-style connection info string, for example
63 hostaddr=127\&.0\&.0\&.1 port=5432 dbname=mydb user=postgres password=mypasswd options=\-csearch_path=\&. For details see
64 Section\ \&32.1.1\&. Alternatively, the name of a foreign server\&.
68 Returns status, which is always
70 (since any error causes the function to throw an error instead of returning)\&.
73 If untrusted users have access to a database that has not adopted a
74 secure schema usage pattern, begin each session by removing publicly\-writable schemas from
75 \fIsearch_path\fR\&. One could, for example, add
76 options=\-csearch_path=
78 \fIconnstr\fR\&. This consideration is not specific to
79 dblink; it applies to every interface for executing arbitrary SQL commands\&.
81 The foreign\-data wrapper
83 has an additional Boolean option
87 will use the SCRAM pass\-through authentication to connect to the remote database\&. With SCRAM pass\-through authentication,
89 uses SCRAM\-hashed secrets instead of plain\-text user passwords to connect to the remote server\&. This avoids storing plain\-text user passwords in PostgreSQL system catalogs\&. See the documentation of the equivalent
91 option of postgres_fdw for further details and restrictions\&.
93 Only superusers may use
95 to create connections that use neither password authentication, SCRAM pass\-through, nor GSSAPI\-authentication\&. If non\-superusers need this capability, use
96 \fBdblink_connect_u\fR
99 It is unwise to choose connection names that contain equal signs, as this opens a risk of confusion with connection info strings in other
108 SELECT dblink_connect(\*(Aqdbname=postgres options=\-csearch_path=\*(Aq);
110 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
114 SELECT dblink_connect(\*(Aqmyconn\*(Aq, \*(Aqdbname=postgres options=\-csearch_path=\*(Aq);
116 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
120 \-\- FOREIGN DATA WRAPPER functionality
121 \-\- Note: local connections that don\*(Aqt use SCRAM pass\-through require password
122 \-\- authentication for this to work properly\&. Otherwise, you will receive
123 \-\- the following error from dblink_connect():
124 \-\- ERROR: password is required
125 \-\- DETAIL: Non\-superuser cannot connect if the server does not request a password\&.
126 \-\- HINT: Target server\*(Aqs authentication method must be changed\&.
128 CREATE SERVER fdtest FOREIGN DATA WRAPPER dblink_fdw OPTIONS (hostaddr \*(Aq127\&.0\&.0\&.1\*(Aq, dbname \*(Aqcontrib_regression\*(Aq);
130 CREATE USER regress_dblink_user WITH PASSWORD \*(Aqsecret\*(Aq;
131 CREATE USER MAPPING FOR regress_dblink_user SERVER fdtest OPTIONS (user \*(Aqregress_dblink_user\*(Aq, password \*(Aqsecret\*(Aq);
132 GRANT USAGE ON FOREIGN SERVER fdtest TO regress_dblink_user;
133 GRANT SELECT ON TABLE foo TO regress_dblink_user;
135 \eset ORIGINAL_USER :USER
136 \ec \- regress_dblink_user
137 SELECT dblink_connect(\*(Aqmyconn\*(Aq, \*(Aqfdtest\*(Aq);
139 \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
143 SELECT * FROM dblink(\*(Aqmyconn\*(Aq, \*(AqSELECT * FROM foo\*(Aq) AS t(a int, b text, c text[]);
145 \-\-\-\-+\-\-\-+\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-
156 10 | k | {a10,b10,c10}
159 \ec \- :ORIGINAL_USER
160 REVOKE USAGE ON FOREIGN SERVER fdtest FROM regress_dblink_user;
161 REVOKE SELECT ON TABLE foo FROM regress_dblink_user;
162 DROP USER MAPPING FOR regress_dblink_user SERVER fdtest;
163 DROP USER regress_dblink_user;