0

Using postgres I have wrote a query to list the tables inside a database

SELECT table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema NOT IN
('pg_catalog', 'information_schema');

Query returns results from the wrong database. It automatically selects the first database from the list of databases in postgres.

How do I specify the database to query? 'j220190_data' being the database to query

I've tried things like:

    SELECT table_name
FROM information_schema.tables
WHERE Databases = 'j220190_data'
AND table_schema NOT IN
('pg_catalog', 'information_schema');

SELECT table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE' AND
WHERE Databases = 'j220190_data'
AND table_schema NOT IN
('pg_catalog', 'information_schema');
Vivek S.
  • 17,862
  • 6
  • 63
  • 80
John
  • 3,853
  • 20
  • 69
  • 146

2 Answers2

0

When you connect to postgres select database;
for example:

psql mydb

and after you post the select, because when you try whit this select, postgres checks on default database.

el fuser
  • 556
  • 1
  • 5
  • 10
  • 1
    @John: [psql](https://www.postgresql.org/docs/current/static/reference-client.html) is a **command line** program, not a SQL statement. – a_horse_with_no_name Nov 28 '16 at 10:59
  • when u connect at postgres db, you can connect at db that you want, you try with psql j220190_data, if your database is this. You connect from shell obviously – el fuser Nov 28 '16 at 11:00
0

If I understood correctly you can query another database using dblink().

Create extension CREATE EXTENSION dblink;

SELECT tbl.*
FROM dblink('dbname=DB1 port=5432   
            host=localhost user=usr password=123', 'SELECT table_name
FROM information_schema.tables
WHERE table_type = ''BASE TABLE'' 
AND table_schema NOT IN(''pg_catalog'', ''information_schema'');') 
AS tbl(table_name varchar(30));
Vivek S.
  • 17,862
  • 6
  • 63
  • 80