1

Does Azure DB for Postgres Server support cross-database queries? Azure SQL database doesn't support cross database queries where Azure SQL managed instance and elastic pool supports cross DB queries!

2
  • Why do you need that? Can't you just create multiple schemas? Commented Apr 27, 2019 at 14:11
  • thats because of multi tenancy ... the separateness is a business call and requirement! Commented Apr 27, 2019 at 17:31

1 Answer 1

2

If you disabled "Deny public network access" & and enabled "Allow access to other Azure resources" under "Connection security" in your Azure Postgres server, you can use the dblink extension:

CREATE EXTENSION dblink;

SELECT dblink_connect('[connection_alias]','host=[azure_pg_hostname] port=5432
dbname=[other_db_name] user=[username]@[azure_pg_hostname] password=[password] sslmode=require');

SELECT * FROM dblink('[connection_alias]', 'select [field1], [field2] from public.[remote_table_name]') 
AS t([field1] [data type], [field2] [data type]);

or the foreign data wrapper extension:

CREATE EXTENSION postgres_fdw;

CREATE SERVER [connection_alias]
FOREIGN DATA WRAPPER postgres_fdw  
OPTIONS (host '[azure_pg_hostname]', dbname '[other_db_name]', port '5432');

CREATE USER MAPPING FOR ([user_name] | CURRENT_USER)
SERVER [connection_alias]
OPTIONS (user '[user_name]@[azure_pg_hostname]', password '[password]');

CREATE FOREIGN TABLE [local_table_name] ( 
[field1] [data type], [field2] [data type]
)
SERVER [connection_alias]
OPTIONS (schema_name 'public', table_name '[remote_table_name]');

SELECT * from [local_table_name];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.