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?user330315– user3303152019-04-27 14:11:22 +00:00Commented Apr 27, 2019 at 14:11
-
thats because of multi tenancy ... the separateness is a business call and requirement!kalyan– kalyan2019-04-27 17:31:03 +00:00Commented Apr 27, 2019 at 17:31
Add a comment
|
1 Answer
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];