In PostgreSQL the system determines which table is meant by following a search path, which is a list of schemas to look in.
The first matching table in the search path is taken to be the one wanted, otherwise, if there is no match a error is raised, even if matching table names exist in other schemas in the database.
To show the current search path you can use the following command:
SHOW search_path;
And to put the new schema in the path, you could use:
SET search_path TO myschema;
Or if you want multiple schemas:
SET search_path TO myschema, public;
Reference: https://www.postgresql.org/docs/current/static/ddl-schemas.html
Answer from Ciro Pedrini on Stack OverflowIn PostgreSQL the system determines which table is meant by following a search path, which is a list of schemas to look in.
The first matching table in the search path is taken to be the one wanted, otherwise, if there is no match a error is raised, even if matching table names exist in other schemas in the database.
To show the current search path you can use the following command:
SHOW search_path;
And to put the new schema in the path, you could use:
SET search_path TO myschema;
Or if you want multiple schemas:
SET search_path TO myschema, public;
Reference: https://www.postgresql.org/docs/current/static/ddl-schemas.html
\l - Display database
\c - Connect to database
\dn - List schemas
\dt - List tables inside public schemas
\dt schema1.* - List tables inside a particular schema.
For example: 'schema1'.
How can I list all schemas in PostgreSQL and understand their properties?
How can I make PostgreSQL schema changes without causing downtime?
What is schema drift in PostgreSQL and why is it such a critical issue?
To lists all schemas, use the (ANSI) standard INFORMATION_SCHEMA
select schema_name
from information_schema.schemata;
More details in the manual
alternatively:
select nspname
from pg_catalog.pg_namespace;
More details about pg_catalog in the manual
When using the psql command line, you may list all schema with command \dn.
For example I have a database with 2 schemas, public & shop and I want to select the table of a column
SELECT * FROM payment
and I get this error SQL state: 42P01
Yet if I use the Schema shop
SELECT * FROM shop.payment
There is no error,
Yet on other database that I am working with that has also 1 schema and public, yet I don't need to specify the schema name, why is this?
For example
SELECT * FROM payment
works perfectly in that database.
Why is this?
These list all schemas including system's in the current database:
\dnS
\dn *
These list all schemas including system's in the current database in detail:
\dnS+
\dn+ *
This lists all schemas excluding system's in the current database:
\dn
This lists all schemas excluding system's in the current database in detail:
\dn+
These also list all schemas including system's in the current database:
SELECT * FROM pg_namespace;
SELECT * FROM information_schema.schemata;
several points:
- If you want to see what query is used for psql shortcut, run
psql -E(with -E key) select *from pg_namespacewill give you list of schemasselect * from pg_class limit where relnamespace = 'your_schema'::regnamespacewill give you all schema relationsselect * from pg_class limit where relnamespace = 'your_schema'::regnamespace and relkind = 'r'will limit the list to tables onlyto limit list of schemas to owned ones only, use
select *from pg_namespace where nspowner = current_user::regrole;