1DBLINK(3)                PostgreSQL 12.2 Documentation               DBLINK(3)
2
3
4

NAME

6       dblink - executes a query in a remote database
7

SYNOPSIS

9       dblink(text connname, text sql [, bool fail_on_error]) returns setof record
10       dblink(text connstr, text sql [, bool fail_on_error]) returns setof record
11       dblink(text sql [, bool fail_on_error]) returns setof record
12

DESCRIPTION

14       dblink executes a query (usually a SELECT, but it can be any SQL
15       statement that returns rows) in a remote database.
16
17       When two text arguments are given, the first one is first looked up as
18       a persistent connection's name; if found, the command is executed on
19       that connection. If not found, the first argument is treated as a
20       connection info string as for dblink_connect, and the indicated
21       connection is made just for the duration of this command.
22

ARGUMENTS

24       connname
25           Name of the connection to use; omit this parameter to use the
26           unnamed connection.
27
28       connstr
29           A connection info string, as previously described for
30           dblink_connect.
31
32       sql
33           The SQL query that you wish to execute in the remote database, for
34           example select * from foo.
35
36       fail_on_error
37           If true (the default when omitted) then an error thrown on the
38           remote side of the connection causes an error to also be thrown
39           locally. If false, the remote error is locally reported as a
40           NOTICE, and the function returns no rows.
41

RETURN VALUE

43       The function returns the row(s) produced by the query. Since dblink can
44       be used with any query, it is declared to return record, rather than
45       specifying any particular set of columns. This means that you must
46       specify the expected set of columns in the calling query — otherwise
47       PostgreSQL would not know what to expect. Here is an example:
48
49           SELECT *
50               FROM dblink('dbname=mydb options=-csearch_path=',
51                           'select proname, prosrc from pg_proc')
52                 AS t1(proname name, prosrc text)
53               WHERE proname LIKE 'bytea%';
54
55       The “alias” part of the FROM clause must specify the column names and
56       types that the function will return. (Specifying column names in an
57       alias is actually standard SQL syntax, but specifying column types is a
58       PostgreSQL extension.) This allows the system to understand what *
59       should expand to, and what proname in the WHERE clause refers to, in
60       advance of trying to execute the function. At run time, an error will
61       be thrown if the actual query result from the remote database does not
62       have the same number of columns shown in the FROM clause. The column
63       names need not match, however, and dblink does not insist on exact type
64       matches either. It will succeed so long as the returned data strings
65       are valid input for the column type declared in the FROM clause.
66

NOTES

68       A convenient way to use dblink with predetermined queries is to create
69       a view. This allows the column type information to be buried in the
70       view, instead of having to spell it out in every query. For example,
71
72           CREATE VIEW myremote_pg_proc AS
73             SELECT *
74               FROM dblink('dbname=postgres options=-csearch_path=',
75                           'select proname, prosrc from pg_proc')
76               AS t1(proname name, prosrc text);
77
78           SELECT * FROM myremote_pg_proc WHERE proname LIKE 'bytea%';
79

EXAMPLES

81           SELECT * FROM dblink('dbname=postgres options=-csearch_path=',
82                                'select proname, prosrc from pg_proc')
83             AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
84             proname   |   prosrc
85           ------------+------------
86            byteacat   | byteacat
87            byteaeq    | byteaeq
88            bytealt    | bytealt
89            byteale    | byteale
90            byteagt    | byteagt
91            byteage    | byteage
92            byteane    | byteane
93            byteacmp   | byteacmp
94            bytealike  | bytealike
95            byteanlike | byteanlike
96            byteain    | byteain
97            byteaout   | byteaout
98           (12 rows)
99
100           SELECT dblink_connect('dbname=postgres options=-csearch_path=');
101            dblink_connect
102           ----------------
103            OK
104           (1 row)
105
106           SELECT * FROM dblink('select proname, prosrc from pg_proc')
107             AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
108             proname   |   prosrc
109           ------------+------------
110            byteacat   | byteacat
111            byteaeq    | byteaeq
112            bytealt    | bytealt
113            byteale    | byteale
114            byteagt    | byteagt
115            byteage    | byteage
116            byteane    | byteane
117            byteacmp   | byteacmp
118            bytealike  | bytealike
119            byteanlike | byteanlike
120            byteain    | byteain
121            byteaout   | byteaout
122           (12 rows)
123
124           SELECT dblink_connect('myconn', 'dbname=regression options=-csearch_path=');
125            dblink_connect
126           ----------------
127            OK
128           (1 row)
129
130           SELECT * FROM dblink('myconn', 'select proname, prosrc from pg_proc')
131             AS t1(proname name, prosrc text) WHERE proname LIKE 'bytea%';
132             proname   |   prosrc
133           ------------+------------
134            bytearecv  | bytearecv
135            byteasend  | byteasend
136            byteale    | byteale
137            byteagt    | byteagt
138            byteage    | byteage
139            byteane    | byteane
140            byteacmp   | byteacmp
141            bytealike  | bytealike
142            byteanlike | byteanlike
143            byteacat   | byteacat
144            byteaeq    | byteaeq
145            bytealt    | bytealt
146            byteain    | byteain
147            byteaout   | byteaout
148           (14 rows)
149
150
151
152PostgreSQL 12.2                      2020                            DBLINK(3)
Impressum