http://www.perlmonks.org?node_id=939369

ansh batra has asked for the wisdom of the Perl Monks concerning the following question:

hi monks
how can i get the list of all the schemas present in a postgres database?
  • Comment on to get the list of schemas in postgresql db

Replies are listed 'Best First'.
Re: to get the list of schemas in postgresql db
by zwon (Abbot) on Nov 22, 2011 at 06:29 UTC

    The same way you will do it without perl, select * from pg_namespace I guess.

      ur query is correct zwon but for that i need to connect to a database first
      cant we supply the database name in the query itself?? thanks

        Easy answer :) : No


        Enjoy, Have FUN! H.Merijn
Re: to get the list of schemas in postgresql db
by erix (Prior) on Dec 04, 2011 at 11:27 UTC

    But TMTOWTDI (SQL standard compliant):

    select schema_name from information_schema.schemata where schema_name <> 'information_schema' -- exclude 'system' schemata and schema_name !~ E'^pg_' -- exclude more 'system' (pg +-specific)

    In psql there is \dn to retrieve schema names. If PGPASSFILE is set (and possibly more PG* environment variables like PGDATABASE), you can just talk to the database from the commandline:

    echo '\dn' | psql