Tiger Cloud: Performance, Scale, Enterprise, Free
Self-hosted products
MST
The dblink Postgres extension allows you to connect to
other Postgres 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, sign in to your Managed Service for TimescaleDB service,
navigate to the Overview tab, and take a note of these parameters for the
Postgres 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.
To enable the dblink extension on an MST Postgres MST service:
Connect to the database as the
tsdbadminuser:psql -x "postgres://tsdbadmin:<PASSWORD>@<HOSTNAME>:<PORT>/defaultdb?sslmode=require"Create the
dblinkextensionCREATE 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 user
user1who can access thedblinkCREATE USER user1 PASSWORD 'secret1'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 for the
user1to automatically authenticate as thetsdbadminwhen using thedblink:CREATE USER MAPPING FOR user1SERVER mst_remoteOPTIONS (user 'tsdbadmin',password 'PASSWORD');Enable
user1to use the remote Postgres connectionmst_remote:GRANT USAGE ON FOREIGN SERVER mst_remote TO user1;
In this example in the user1 user queries the remote table inventory defined
in the target Postgres database from the mst_remote server definition:
To query a foreign data wrapper, you must be a database user with the necessary permissions on the remote server.
Connect to the Managed Service for TimescaleDB service as
user1with necessary grants to the remote server.Establish the
dblinkconnection to the remote target server:SELECT dblink_connect('my_new_conn', 'mst_remote');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)
Found an issue on this page?Report an issue or Edit this page
in GitHub.