Postgres get current schema name - postgresql

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'
)

Related

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'
)

Call to Query with args [], was not expected in go-sqlmock for compound SQL queries

I was able to successfully mock the query to select from one table like so:
sqlMock.ExpectQuery("^SELECT DISTINCT (.+) FROM myTable1, myTable2").
WillReturnRows(myResultRows)
But I was not able to mock the following query that checks for the existence of the table in my postgres db:
SELECT EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'myTable3' );
The combination of:
existsRows := sqlmock.NewRows([]string{"exists"}).
AddRow(true)
AND
slMock.ExpectQuery("^SELECT EXISTS").
WillReturnRows(existsRows)
I tried mocking SELECT 1 as well but I get the exact same error:
time="2019-09-27T15:49:41-07:00" level=panic msg="db query" error="call to Query 'SELECT EXISTS\n\t\t( SELECT 1\n\t\tFROM information_schema.tables\n\t\tWHERE table_schema = 'public'\n\t\t AND table_name = 'myTable3' );' with args [], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which......
Packages I am using:
import (
"database/sql"
"db"
"os"
"testing"
// not explicitly called
_ "github.com/denisenkom/go-mssqldb"
_ "github.com/lib/pq"
"github.com/DATA-DOG/go-sqlmock"
"github.com/sirupsen/logrus"
)
Any ideas or pointers are appreciated. I couldn't find relevant examples on the internet
Actually,
sqlMock.ExpectQuery("SELECT EXISTS \\( SELECT 1 FROM information_schema\\.tables WHERE table_schema = 'public' AND table_name = 'myTable3' \\);").
WillReturnRows(existsRows)
did the trick.
More examples here:
https://chromium.googlesource.com/external/github.com/DATA-DOG/go-sqlmock/+/e36ad8d068217ee8e4df50408476b153e115e3e6/README.md
I also used regex101.com
The clue was it was expecting the next query straightaway. So we knew it didn't read this one at all. My co worker pointed it out :)
I am not sure but think the problem is in your indentation of ur query try to remove the line break or your tabulation in your query SELECT EXISTS
( SELECT 1
FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'myTable3' );
Like this SELECT EXISTS( SELECT 1 FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'myTable3' );

PostgreSQL how to check table existence?

This is how we can check table existence in MSSQL:
IF OBJECT_ID(N'public."TABLE_NAME"', N'U') IS NOT NULL
select 1 as 'column'
else
select 0 as 'column';
which stores outcome in variable 'column'
How can I do same thing in PostgreSQL ? I want to return 1 or 0 for respective outcome.
Use a SELECT with an EXISTS operator checking e.g. information_schema.tables:
select exists (select *
from information_schema.tables
where table_name = 'table_name'
and table_schema = 'public') as table_exists;
If you can't (or won't) deal with proper boolean values, the simply cast the result to a number (but I have no idea why that should be better):
select exists (select *
from information_schema.tables
where table_name = 'table_name'
and table_schema = 'public')::int as "column";
Note that column is a reserved keyword and thus you need to quote it using double quotes.
Check for column in a table existence use view pg_tables
IF EXISTS ( SELECT attname
FROM pg_attribute
WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'YOURTABLENAME')
AND attname = 'YOURCOLUMNNAME')
THEN
-- do something
END IF;
For my sql use INFORMATION_SCHEMA.COLUMNS
SELECT 1
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'tbl_name'
[AND table_schema = 'db_name']
[AND column_name LIKE 'wild']

PostgreSQL - Combine select

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');

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'