JDBC MS SQL Server how to get List of Schema from another Database in the same server - mssql-jdbc

I can get all databases in the same server with the function databaseMetaData.getCatalogs() and all the tables in any database in any schema with the function databaseMetaData.getTables("databaseName", "schemaName", null, null).
databaseMetaData.getSchemas() function provides only the schema names of connected database given in the connection String. Is there a way to get schema names of other databases in the same server?

Related

Postgres for Airflow backend DB to create metadata tables in a specified schema

I'm setting up postgres backend for Airflow metadata DB using the following sqlalchemy connection string:
sql_alchemy_conn = postgresql+psycopg2://<user>:<pass>#<host>:5432/<db>
When I run initdb/upgradedb, the tables are created in public schema.
Is there a way to create the tables in a specified schema?
Can the schema where I would like the tables to be created be specified as part of the connection string?
From the docs:
Also note that since SqlAlchemy does not expose a way to target a specific schema in the Postgres connection URI, you may want to set a default schema for your role with a command similar to ALTER ROLE username SET search_path = airflow, foobar;
So, connect to postgres, and set the search path for your user.

Connect to Hive database tables using postgresql-fdw from postgresql server

Cant connect from postgresql server to hive databases on remote server.
Tried using the following queries but doenst work. Connection established but no response back from hive server.
--create extension postgres_fdw;
DROP USER MAPPING IF EXISTS FOR CURRENT_USER SERVER data_db;
drop server data_db;
create server data_db
foreign data wrapper postgres_fdw
options (host 'net.com' , port 'hiveport' , SSLMODE 'allow', dbname 'datah');
create user mapping for current_user
server data_db
options(user 'user', password 'password');
drop schema app;
create schema app;
import foreign schema public
from server data_db
into app;
The result was unknown expecting authentication request from the server (SQL state 08001).
I would like to be able to establish a connection to the hive database clusters using the fdw from postgresql to import selective data.
Thank you very much in advance and best regards!
postgres_fdw is for connecting to other PostgreSQL instances. Hive doesn't use the same wire protocol as PostgreSQL does, so surely postgres_fdw would not be expected to connect to it successfully. Perhaps you can try https://github.com/youngwookim/hive-fdw-for-postgresql or https://sourceforge.net/projects/hadoopfdw/.

How to add a remote Postgresql db(linked server) to a Postgresql db?

I have a postgresql db at home and one on the cloud. I'd like to add my home db to the cloud db so I can query easily between databases. How can this be done? Without using dblink http://www.postgresonline.com/journal/archives/44-Using-DbLink-to-access-other-PostgreSQL-Databases-and-Servers.html
My home db will use a dynamic ip provider (can I add a dynamic ip address such as myhomedb.dedyn.io to postgresql settings?)
I'm stating all this in case there are any issues. My home db will only be used to update massive amount of data but isn't mission critical (as we know cloud computing isn't cheap).
Thanks in advance.
Looks like postgres-fdw is the way to go: https://www.postgresql.org/docs/current/postgres-fdw.html
First install the extension:
CREATE EXTENSION postgres_fdw;
Then create a foreign server using CREATE SERVER. In this example we
wish to connect to a PostgreSQL server on host 192.83.123.89
listening on port 5432. The database to which the connection is made
is named foreign_db on the remote server:
CREATE SERVER foreign_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '192.83.123.89', port '5432', dbname 'foreign_db');
A user mapping, defined with CREATE USER MAPPING, is needed as well
to identify the role that will be used on the remote server:
CREATE USER MAPPING FOR local_user
SERVER foreign_server
OPTIONS (user 'foreign_user', password 'password');
Now it is possible to create a foreign table with CREATE FOREIGN
TABLE. In this example we wish to access the table named
some_schema.some_table on the remote server. The local name for it
will be foreign_table:
CREATE FOREIGN TABLE foreign_table (
id integer NOT NULL,
data text
)
SERVER foreign_server
OPTIONS (schema_name 'some_schema', table_name 'some_table');
It's essential that the data types and other properties of the columns
declared in CREATE FOREIGN TABLE match the actual remote table.
Column names must match as well, unless you attach column_name options
to the individual columns to show how they are named in the remote
table. In many cases, use of IMPORT FOREIGN SCHEMA is preferable to
constructing foreign table definitions manually.

How to access SQL server data from PostgreSQL (pgAdmin)?

Need to access the SQL server data from pgAdmin.
I have tried with foreign data wrapper, but it's not working.
I'm trying to access the data from " dbo.example " which is in SQL server from pgAdmin.
Please provide the prerequisites to achieve this.
Sample Code:
CREATE EXTENSION postgres_fdw;
CREATE SERVER mssql_pm_server
FOREIGN DATA WRAPPER postgres_fdw
OPTIONS (host '121.112.141.88', port '1874');
CREATE USER MAPPING FOR postgres
SERVER mssql_pm_server
OPTIONS ( user '1122', password '1122',dbname 'db_Product');
CREATE FOREIGN TABLE mssql_pm_table1(
BusinessEntityID int NOT NULL,
TerritoryID text
)
SERVER mssql_pm_server
OPTIONS (table_name 'dbo.example');
Error:
ERROR: could not connect to server "mssql_pm_server"
DETAIL: server closed the connection unexpectedly This probably
means the server terminated abnormally before or while processing the
request.
SQL state: 08001
You need the proper foreign data wrapper to access Microsoft SQL server. The PostgreSQL foreign data wrapper is to connect to PostgreSQL databases.
You should use tds_fdw.

Create schema dynamically connected to PostgreSql in CakePHP 2.6

For my application I need to create a schema with company name in PostgreSQL database dynamically.
How to execute PostgreSQL query CREATE SCHEMA name in cakephp.