PostgreSQL - Combine select - postgresql

I have a database which stores multiple schemas with tables in it
I want to get every schema name and in the same time check if the schema has a table named 'status'
I got two queries for that:
This query returns all schemas of the database:
select schema_name from information_schema.schemata
With the returned query I then check every schema if the table 'status' exists:
select exists(select * from information_schema.tables where table_schema = 'the_schema_name' and table_name = 'status')
My question is now if I can combine these two queries into one?
Thanks in advance
Doobie

Use a co-related sub-query:
select s.schema_name,
exists (select *
from information_schema.tables t
where t.table_schema = s.schema_name
and t.table_name = 'status') as status_exists
from information_schema.schemata s;
If you just want to find the schemas where the table does not exist, you can do that with the following query:
select s.schema_name
from information_schema.schemata s
where not exists (select *
from information_schema.table t
where t.schema_name = s.schema_name
and t.table_name = 'status');

Related

PostgreSQL : How to select rows from multiple tables inside different schemas?

I am trying to select all rows from multiple tables inside various schemas.
These are the tables inside different schemas
schema_1-->ABC_table_1,XYZ_table_2
schema_2-->ABC_table_1,JLK_table_2
.
.
schema_N-->ABC_table_1,LMN_table_2
I am trying to select all rows from table_2 from all schemas:
This query is giving me all the tables:
SELECT
table_schema || '.' || table_name
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema NOT IN ('pg_catalog', 'information_schema');
What i need to do is something like
select * from schema_1.XYZ_table_2
Union all
select * from schema_2.JLK_table_2
.
.
schema_2.LMN_table_2
Verify if this option can work for you :
select row_to_json(row) from (select * from table1 ) row
UNION ALL
select row_to_json(row) from (select * from table2 ) row
;
If you dont want to work with JSON/HStore datamanipulation you can also try to make dynamic sql select creation for all possible columns in the schema then UNION ALL can still work .
Json looks pretty easier to do this task and also very supported to do any further actions.

Postgres get current schema name

I want to get my current connection schema name I found that 'show search_path' returns the needed result but I need to use this result in the query.
How can I use "show search_path" in a Postgres query?
if not (SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE
table_name = 'customer' and table_schema = **show search_path** )) then
do something ....
end
I need to use table_schema = show search_path
Use the current_schema function. It gives you the first schema on the search_path that actually exists, that is, the schema where unqualified tables will be created.
SELECT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_name = 'project_customer_scheme'
AND table_schema = (
SELECT setting FROM pg_settings WHERE name = 'search_path'
)

PostgreSQL information schema query for tables without a specific data type

I'm trying to write a PostgreSQL query to get all the tables (from a specified schema) which don't have any columns of a specific data type, for example, show all tables without any integer type columns. so far I can manage to get only the table names, the data types of the columns they have and their count but I feel like that's the wrong direction in order to get what I want. any help appreciated, thanks
SELECT Table_Name, Data_Type, COUNT(Data_Type)
FROM Information_schema.Columns
WHERE Table_Schema = 'project'
GROUP BY Table_Name, Data_Type
You'll want to start with the tables table and then use an EXISTS subquery:
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'project'
AND NOT EXISTS (
SELECT *
FROM information_schema.columns
WHERE column.table_schema = tables.table_schema
AND column.table_name = tables.table_name
AND data_type = 'integer'
)

Select all tables which begin with "coc"

Select all tables which begin with "coc"
I wanna know how to select all tables which begin with "coc". There are coc1, coc2, coc3.... tables in DB.
SELECT * FROM coc...
You can do it as follows:
SELECT *
FROM coc1
UNION
SELECT *
FROM coc2
UNION
SELECT *
FROM coc3
or
SELECT
TABLE_NAME
FROM
information_schema.TABLES
WHERE
table_schema = [DB_NAME]
AND
TABLE_NAME LIKE 'coc%'

SELECT ALL column_names in postgresql

I'm using PostgreSQL and I want to create a query that will display all column_names in a specific table.
Schema: codes
Table Name: watch_list
Here are the column_names in my table:
watch_list_id, watch_name, watch_description
I tried what I found in the web:
SELECT *
FROM information_schema.columns
WHERE table_schema = 'codes'
AND table_name = 'watch_list'
It output is not what I wanted. It should be:
watch_list_id, watch_name, watch_description
How to do this?
If you want all column names in a single row, you need to aggregate those names:
SELECT table_name, string_agg(column_name, ', ' order by ordinal_position) as columns
FROM information_schema.columns
WHERE table_schema = 'codes'
AND table_name = 'watch_list'
GROUP BY table_name;
If you remove the condition on the table name, you get this for all tables in that schema.
SELECT table_name FROM information_schema.tables WHERE table_schema='public'