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?
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.