Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any shortcut for using dblink in Postgres?

In Postgres, you can link to your other databases using dblink, but the syntax is very verbose. For example you can do:

SELECT *
FROM dblink (
    'dbname=name port=1234 host=host user=user password=password',
    'select * from table'
) AS users([insert each column name and its type here]);

Is there any way to do this faster? Maybe pre-define the connections?

I noticed that Postgres has a new create foreign table function for connecting to a MySQL database. It has a simpler syntax than dblink. Could I use that?

like image 954
Joe Avatar asked Oct 19 '11 22:10

Joe


People also ask

How do I create a dblink in PostgreSQL?

Load the dblink extension into PostgreSQL. CREATE EXTENSION dblink; Create a persistent connection to a remote PostgreSQL database using the dblink_connect function specifying a connection name (myconn), database name (postgresql), port (5432), host (hostname), user (username) and password (password).

How does dblink work in PostgreSQL?

dblink executes a query (usually a SELECT , but it can be any SQL statement that returns rows) in a remote database. When two text arguments are given, the first one is first looked up as a persistent connection's name; if found, the command is executed on that connection.

Does PostgreSQL support dblink?

dblink is a module that supports connections to other PostgreSQL databases from within a database session.


2 Answers

In PostgreSQL 8.4 and later, you can use the foreign data wrapper functionality to define the connection parameters. Then you'd simply refer to the foreign server name in your dblink commands. See the example in the documentation.

In PostgreSQL 9.1 and later, you can use the foreign data wrapper functionality to define foreign tables and thus access remote databases transparently, without using dblink at all. For that, you'd need to get the postgresql_fdw wrapper, which isn't included in any production release yet, but you can find experimental code in the internet. In practice, this route is more of a future option.

like image 73
Peter Eisentraut Avatar answered Sep 22 '22 16:09

Peter Eisentraut


You can wrap connection parameters in a FOREIGN SERVER object like @Peter explains, but you'd still have to spell out the rest.

You can encapsulate everything in a view or function, so you type it only once. Example with a function - Run as superuser:

CREATE OR REPLACE FUNCTION f_lnk_tbl()
  RETURNS TABLE(tbl_id int, col1 text, log_ts timestamp) AS
$BODY$

SELECT *
  FROM dblink(
  'SELECT tbl_id, col1, log_ts
   FROM   tbl
   ORDER  BY tbl_id'::text) AS b(
 tbl_id int
,col1   text
,log_ts timestamp);

$BODY$ LANGUAGE sql STABLE SECURITY DEFINER;
REVOKE ALL ON FUNCTION f_lnk_tbl() FROM public;


CREATE OR REPLACE FUNCTION f_sync()
  RETURNS text AS
$BODY$

SELECT dblink_connect('hostaddr=123.123.123.123 port=5432 dbname=mydb
                       user=postgres password=*secret*');

INSERT INTO my_local_tbl SELECT * FROM f_lnk_tbl();
-- more tables?

SELECT dblink_disconnect();

$BODY$
  LANGUAGE sql VOLATILE SECURITY DEFINER;
REVOKE ALL ON FUNCTION blob.f_dbsync() FROM public;
-- GRANT ....;
like image 42
Erwin Brandstetter Avatar answered Sep 19 '22 16:09

Erwin Brandstetter