sandevfares
sandevfares

Reputation: 245

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

Upvotes: 5

Views: 10214

Answers (3)

Hemanth
Hemanth

Reputation: 11

select current_schema; -- This should give you the current schema

Upvotes: 1

Laurenz Albe
Laurenz Albe

Reputation: 246023

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.

Upvotes: 12

sandevfares
sandevfares

Reputation: 245

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

Upvotes: 1

Related Questions