Tutorial

PostgreSQL List Schemas in a Database: Use \dn

To list schemas in a PostgreSQL database, use `\dn` for an interactive check. Use SQL for scripts; both show schemas in the database you’re connected to.

Key takeaways

  • Connect to the database you want to inspect, then run \dn at the psql prompt to list its schemas.
  • Use information_schema.schemata when a script or another SQL client needs schema names.
  • Query pg_catalog.pg_namespace when you need a fuller look at PostgreSQL’s namespace catalog.
  • If a schema looks missing, check the connected database, the role’s privileges, and any system-schema filter.

For PostgreSQL list schemas in database, my recommendation is \dn for a quick check at the psql prompt. If another client or a script needs the names, use SQL against information_schema.schemata instead.

PostgreSQL list schemas in database: use \dn

Use \dn to list schemas in the database you’re connected to. I’d start there when I’m inspecting a database by hand; the SQL alternatives are for output a program must consume or for a closer look at PostgreSQL’s catalog.

A schema is a namespace for objects such as tables and views, not a separate database. As Devart’s guide to listing PostgreSQL schemas explains, a database has a public schema by default and can have additional schemas. Keep the database boundary in mind before treating a short result as a missing schema.

List schemas in psql

Connect to the database you want to inspect, then run \dn at the psql prompt. Here are the two actions in order; replace the connection placeholders with your own details.

  1. In your shell, connect to the target database:
  2. At the psql prompt, list its schemas:
psql -U your_username -h your_host -d your_database
\dn

\dn lists schemas in the connected database, not every database in the cluster. It is a psql meta-command rather than SQL, so don’t put it into an application query. If the list looks wrong, check which database you connected to before changing the query.

List schemas with SQL—and hide system schemas

Query information_schema.schemata when you need schema names as SQL results. Run this in your SQL client against the database you want to inspect; it is the better fit for a script than a psql backslash command.

SELECT schema_name
FROM information_schema.schemata
ORDER BY schema_name;

That unfiltered view can include pg_catalog and information_schema alongside user-defined schemas. If you want the application-facing names, exclude those two and names beginning pg_; the filter below leaves public eligible for the result.

SELECT schema_name
FROM information_schema.schemata
WHERE schema_name NOT IN ('pg_catalog', 'information_schema')
  AND schema_name NOT LIKE 'pg!_%' ESCAPE '!'
ORDER BY schema_name;

The ! makes the underscore in pg_ literal, rather than a pattern wildcard. This filter is a choice about what you want to see, not proof that the excluded schemas do not exist.

  • Names for a script: use the information_schema.schemata query above.
  • Fuller PostgreSQL namespace inspection: query pg_catalog.pg_namespace, for example with SELECT nspname FROM pg_catalog.pg_namespace ORDER BY nspname;.

information_schema.schemata is the view to use for the current role’s SQL results; pg_catalog.pg_namespace is the fuller namespace catalog. A role needs privileges to access a schema, as Erwin Brandstetter notes in PostgreSQL and default Schemas; listing methods can also differ in whether they display system schemas. Use the catalog query when the narrower result is what you’re investigating.

Frequently asked questions

Why is a PostgreSQL schema missing from my results?

First, confirm you connected to the database that contains it: schema listings concern the current database, whereas \l lists databases. Our PostgreSQL database-listing guide covers that check. Then check the role’s privileges and whether your query filtered out a system schema.

Does \dt list schemas in PostgreSQL?

No. \dt lists tables in schemas on the current search path, not the schema names themselves; use \dn for those. For table-specific commands, see our PostgreSQL table-listing guide.

Which schema-listing method should I use?

Use \dn by default when you’re checking a database interactively. Choose information_schema.schemata when SQL needs to run in a script or another client; choose pg_catalog.pg_namespace when the namespace catalog itself is what you need to inspect.

Method When to use it System-schema visibility Main caveat
\dn Interactive psql check System schemas may be hidden by default. psql command, not SQL.
information_schema.schemata Portable or scripted SQL Can include pg_catalog and information_schema. Check the current role and filter unwanted schemas.
pg_catalog.pg_namespace Fuller PostgreSQL catalog inspection Query the namespace catalog, including internal namespaces. PostgreSQL-specific; filter the names you want.

Sources

  1. How to Show List of All Schemas in PostgreSQL Database - Easy Examples, devart.com
  2. PostgreSQL and default Schemas - Database Administrators Stack Exchange, dba.stackexchange.com