1DBLINK_CONNECT(3) PostgreSQL 9.2.24 Documentation DBLINK_CONNECT(3)
2
3
4
6 dblink_connect - opens a persistent connection to a remote database
7
9 dblink_connect(text connstr) returns text
10 dblink_connect(text connname, text connstr) returns text
11
13 dblink_connect() establishes a connection to a remote PostgreSQL
14 database. The server and database to be contacted are identified
15 through a standard libpq connection string. Optionally, a name can be
16 assigned to the connection. Multiple named connections can be open at
17 once, but only one unnamed connection is permitted at a time. The
18 connection will persist until closed or until the database session is
19 ended.
20
21 The connection string may also be the name of an existing foreign
22 server. It is recommended to use the postgresql_fdw_validator when
23 defining the corresponding foreign-data wrapper. See the example below,
24 as well as the following: CREATE FOREIGN DATA WRAPPER
25 (CREATE_FOREIGN_DATA_WRAPPER(7)), CREATE SERVER (CREATE_SERVER(7)),
26 CREATE USER MAPPING (CREATE_USER_MAPPING(7))
27
29 connname
30 The name to use for this connection; if omitted, an unnamed
31 connection is opened, replacing any existing unnamed connection.
32
33 connstr
34 libpq-style connection info string, for example hostaddr=127.0.0.1
35 port=5432 dbname=mydb user=postgres password=mypasswd. For details
36 see PQconnectdb in Section 31.1, “Database Connection Control
37 Functions”, in the documentation.
38
40 Returns status, which is always OK (since any error causes the function
41 to throw an error instead of returning).
42
44 Only superusers may use dblink_connect to create
45 non-password-authenticated connections. If non-superusers need this
46 capability, use dblink_connect_u instead.
47
48 It is unwise to choose connection names that contain equal signs, as
49 this opens a risk of confusion with connection info strings in other
50 dblink functions.
51
53 SELECT dblink_connect('dbname=postgres');
54 dblink_connect
55 ----------------
56 OK
57 (1 row)
58
59 SELECT dblink_connect('myconn', 'dbname=postgres');
60 dblink_connect
61 ----------------
62 OK
63 (1 row)
64
65 -- FOREIGN DATA WRAPPER functionality
66 -- Note: local connection must require password authentication for this to work properly
67 -- Otherwise, you will receive the following error from dblink_connect():
68 -- ----------------------------------------------------------------------
69 -- ERROR: password is required
70 -- DETAIL: Non-superuser cannot connect if the server does not request a password.
71 -- HINT: Target server's authentication method must be changed.
72 CREATE USER dblink_regression_test WITH PASSWORD 'secret';
73 CREATE FOREIGN DATA WRAPPER postgresql VALIDATOR postgresql_fdw_validator;
74 CREATE SERVER fdtest FOREIGN DATA WRAPPER postgresql OPTIONS (hostaddr '127.0.0.1', dbname 'contrib_regression');
75
76 CREATE USER MAPPING FOR dblink_regression_test SERVER fdtest OPTIONS (user 'dblink_regression_test', password 'secret');
77 GRANT USAGE ON FOREIGN SERVER fdtest TO dblink_regression_test;
78 GRANT SELECT ON TABLE foo TO dblink_regression_test;
79
80 \set ORIGINAL_USER :USER
81 \c - dblink_regression_test
82 SELECT dblink_connect('myconn', 'fdtest');
83 dblink_connect
84 ----------------
85 OK
86 (1 row)
87
88 SELECT * FROM dblink('myconn','SELECT * FROM foo') AS t(a int, b text, c text[]);
89 a | b | c
90 ----+---+---------------
91 0 | a | {a0,b0,c0}
92 1 | b | {a1,b1,c1}
93 2 | c | {a2,b2,c2}
94 3 | d | {a3,b3,c3}
95 4 | e | {a4,b4,c4}
96 5 | f | {a5,b5,c5}
97 6 | g | {a6,b6,c6}
98 7 | h | {a7,b7,c7}
99 8 | i | {a8,b8,c8}
100 9 | j | {a9,b9,c9}
101 10 | k | {a10,b10,c10}
102 (11 rows)
103
104 \c - :ORIGINAL_USER
105 REVOKE USAGE ON FOREIGN SERVER fdtest FROM dblink_regression_test;
106 REVOKE SELECT ON TABLE foo FROM dblink_regression_test;
107 DROP USER MAPPING FOR dblink_regression_test SERVER fdtest;
108 DROP USER dblink_regression_test;
109 DROP SERVER fdtest;
110 DROP FOREIGN DATA WRAPPER postgresql;
111
112
113
114PostgreSQL 9.2.24 2017-11-06 DBLINK_CONNECT(3)