Chris Cashwell Chris Cashwell - 3 months ago 57x
SQL Question

Postgresql: Check if Schema Exists?

I need to create, manage and drop schemas on the fly. If I go to create a schema that already exists, I want to (conditionally, via external means) drop and recreate it as specified. How can I check for the existence of said schema on my Postgres 9 server?

Currently, I'm doing this:

select exists (select * from pg_catalog.pg_namespace where nspname = 'schemaname');

but I feel like there's probably another way... is this the "proper" way to query Postgres for the existence of a particular schema?


The following query will tell you whether a schema exists.

SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'name';