-
Notifications
You must be signed in to change notification settings - Fork 9.1k
Expand file tree
/
Copy pathpg_tables.sql
More file actions
41 lines (37 loc) · 1.72 KB
/
pg_tables.sql
File metadata and controls
41 lines (37 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- Create database scoped credential to connect to PostgreSQL server
-- Provide appropriate credentials to PostgreSQL server in below statement.
-- If you are using SQL Server Management Studio then you can replace the parameters using
-- the Query menu, and "Specify Values for Template Parameters" option.
IF NOT EXISTS(SELECT * FROM sys.database_scoped_credentials WHERE name = 'PostgreSQL11-user')
CREATE DATABASE SCOPED CREDENTIAL [PostgreSQL11-user]
WITH IDENTITY = '<postgres_user,nvarchar(100),mssql-user>'
, SECRET = '<postgres_user_password,nvarchar(100),sql19tw0postgresql>';
-- Create external data source that points to PostgreSQL server.
--
IF NOT EXISTS(SELECT * FROM sys.external_data_sources WHERE name = 'PostgreSQL11')
CREATE EXTERNAL DATA SOURCE PostgreSQL11
WITH (LOCATION = 'odbc://<postgres_server,nvarchar(100),postgres-server-name>'
, CONNECTION_OPTIONS = 'Driver={PostgreSQL ODBC Driver(UNICODE)}'
, CREDENTIAL = [PostgreSQL11-user]);
-- Create external table over inventory table on PostgreSQL server
--
IF NOT EXISTS(SELECT * FROM sys.external_tables WHERE name = 'pg_tables')
CREATE EXTERNAL TABLE pg_tables
(
schemaname nvarchar(128) not null,
tablename nvarchar(128) not null,
tableowner nvarchar(128) not null,
tablespace nvarchar(128) not null,
hasindexes nvarchar(5) not null,
hasrules nvarchar(5) not null,
hastriggers nvarchar(5) not null,
rowsecurity nvarchar(5) not null
)
WITH (LOCATION = 'postgres.pg_catalog.pg_tables', DATA_SOURCE = PostgreSQL11);
SELECT * FROM pg_tables;
-- Cleanup
/*
DROP EXTERNAL TABLE pg_tables
DROP EXTERNAL DATA SOURCE PostgreSQL11
DROP DATABASE SCOPED CREDENTIAL [PostgreSQL11-user]
*/