Use the Postgres dblink extension
Use the dblink extension and connect to other Postgres databases
The dblink PostgreSQL extension allows you to connect to
other PostgreSQL databases and to run arbitrary queries.
You can use foreign data wrappers (FDWs) to define a remote
foreign server to access its data. The database connection details such as
hostnames are kept in a single place, and you only need to create a
user mapping to store remote connections credentials.
Before you begin
Section titled “Before you begin”Sign in to your Managed Service for TimescaleDB service,
navigate to the Overview tab, and take a note of these parameters for the
PostgreSQL remote server. Alternatively, you can use the avn service get
command in the Aiven client:
HOSTNAME: The remote database hostnamePORT: The remote database portUSER: The remote database user to connect. The default user istsdbadmin.PASSWORD: The remote database password for theUSERDATABASE_NAME: The remote database name. The default database name isdefaultdb.
Enable the dblink extension
Section titled “Enable the dblink extension”To enable the dblink extension on an MST PostgreSQL MST service:
- Connect to the database
Connect to the database as the
tsdbadminuser:Terminal window psql -x "postgres://tsdbadmin:<PASSWORD>@<HOSTNAME>:<PORT>/defaultdb?sslmode=require" - Create the dblink extension and test data
-
Create the
dblinkextension:CREATE EXTENSION dblink; -
Create a table named
inventory:CREATE TABLE inventory (id int); -
Insert data into the
inventorytable:INSERT INTO inventory (id) VALUES (100), (200), (300);
-
Create a foreign data wrapper using dblink_fdw
Section titled “Create a foreign data wrapper using dblink_fdw”- Create a user
Create a user
user1who can access thedblink:CREATE USER user1 PASSWORD 'secret1' - Create a remote server definition
Create a remote server definition named
mst_remote, usingdblink_fdwand the connection details of the Managed Service for TimescaleDB service:CREATE SERVER mst_remoteFOREIGN DATA WRAPPER dblink_fdwOPTIONS (host 'HOST',dbname 'DATABASE_NAME',port 'PORT'); - Create a user mapping
Create a user mapping for
user1to automatically authenticate astsdbadminwhen using thedblink:CREATE USER MAPPING FOR user1SERVER mst_remoteOPTIONS (user 'tsdbadmin',password 'PASSWORD'); - Grant usage on the remote server
Enable
user1to use the remote PostgreSQL connectionmst_remote:GRANT USAGE ON FOREIGN SERVER mst_remote TO user1;
Query data using a foreign data wrapper
Section titled “Query data using a foreign data wrapper”To query a foreign data wrapper, you must be a database user with the necessary permissions on the remote server.
- Connect to the service
Connect to the Managed Service for TimescaleDB service as
user1with necessary grants to the remote server. - Establish the dblink connection
Establish the
dblinkconnection to the remote target server:SELECT dblink_connect('my_new_conn', 'mst_remote'); - Query using the foreign server definition
Query using the foreign server definition as parameter:
SELECT * FROM dblink('my_new_conn','SELECT * FROM inventory') AS t(a int);Output is similar to:
a-----100200300(3 rows)