PostgreSQL current_schema Function: Usage and Examples
In this article
The current_schema() function in PostgreSQL returns the name of the schema that is currently first in the search path. This is the schema where PostgreSQL will first look for tables, views, and functions when you reference them without a schema qualifier (e.g., calling SELECT * FROM users instead of SELECT * FROM app_data.users). To use it, simply execute SELECT current_schema(); in your SQL editor.
What You'll Need
- A running PostgreSQL database instance.
- A client tool to execute SQL queries (such as psql, pgAdmin, DBeaver, or a custom application connection).
- A user account with permission to connect to the database.
Step-by-Step
- Connect to your database: Open your preferred SQL interface and connect to the specific database you wish to query.
- Check the current schema: Run the following command to see which schema is currently active:
By default, for most new users and databases, this will returnSELECT current_schema();"$user"(which resolves to a schema matching your username) or"public". - Verify the search path: Since
current_schema()only returns the first element of the search path, you may want to see the full list of schemas PostgreSQL searches through. Run:SHOW search_path; - Change the current schema for the session: To change which schema is returned by
current_schema(), you must modify thesearch_path. Use theSETcommand:
In this example,SET search_path TO my_custom_schema, public;my_custom_schemabecomes the current schema. - Confirm the change: Run the function again to verify the update:
The result should now beSELECT current_schema();"my_custom_schema". - Set a permanent schema for a user: If you want a specific user to always start in a certain schema without running a
SETcommand every time they log in, use theALTER ROLEcommand:
Note that this requires superuser or appropriate administrative privileges.ALTER ROLE username SET search_path TO my_custom_schema, public;
Common Mistakes
- Confusing current_schema() with the database name:
current_schema()returns the schema (a namespace within a database), not the name of the database itself. To find the database name, useSELECT current_database();. - Assuming it returns all schemas: This function only returns the first schema in the
search_path. If your path ismyschema, public, it will only returnmyschema, even thoughpublicis also accessible. - Forgetting session scope: Using
SET search_pathonly affects the current database connection. Once you disconnect and reconnect, the path reverts to the default unless you usedALTER ROLE. - Typing errors in schema names: If you
SET search_pathto a schema that does not exist, PostgreSQL will not throw an error immediately. However,current_schema()will return that non-existent name, and your subsequent queries for tables will fail with "relation does not exist" errors.
Tips
- Use for Dynamic SQL:
current_schema()is highly useful in PL/pgSQL functions or scripts where you need to log which schema was active when a specific operation occurred. - The "$user" Placeholder: If you see
"$user"as the result, it means PostgreSQL is looking for a schema that has the exact same name as the logged-in user. If that schema doesn't exist, it moves to the next item in the search path (usuallypublic). - Schema Qualification: While managing the current schema is helpful, in production application code, it is often safer to use "fully qualified names" (e.g.,
schema_name.table_name) to avoid ambiguity and errors caused by unexpected search path changes.
Troubleshooting
If the current_schema() function is returning a value you did not expect, or your queries are not finding the tables you know exist, try these steps:
- Check for typos: Verify the spelling of your schema. Run
\dnin psql or check the "Schemas" tree in pgAdmin to ensure the schema actually exists. - Inspect the full path: Run
SHOW search_path;. If your desired schema is listed second or third,current_schema()will not show it, but the database can still find the tables. Move your desired schema to the front usingSET search_path TO desired_schema, public;. - Verify Permissions: Ensure the user has
USAGEprivileges on the schema. If you cannot access the schema, you may be unable to set it as your current path effectively. UseGRANT USAGE ON SCHEMA schema_name TO username;. - Restart the Session: If you used
ALTER ROLEto change the default schema, the change will not take effect in your current active window. You must disconnect and reconnect to the database for the new default to load.
Frequently Asked Questions
Is current_schema() the same as the search_path?
No. The search_path is a list of all schemas PostgreSQL checks (in order). current_schema() is a function that returns only the first valid schema in that list.
Can I change the current schema without changing the search_path?
No. The current schema is derived directly from the search_path. To change the result of current_schema(), you must modify the search_path using SET or ALTER ROLE.
Why does current_schema() return "public" by default?
PostgreSQL is configured by default to include the public schema in the search path. If no user-specific schema exists, public becomes the primary location for object resolution.
Does changing the current schema affect other users?
If you use SET search_path, it only affects your current session. If you use ALTER ROLE, it only affects that specific user's future sessions. It never affects other active users or different roles.
Can I use current_schema() in a WHERE clause?
Yes. You can use it in any SQL expression, such as SELECT * FROM logs WHERE schema_name = current_schema();, provided the data types match.