logo of Akmatori
23.06.2024

How to List Schemas and Change Them in PostgreSQL

head-image

Managing schemas is a crucial part of working with PostgreSQL. Schemas help organize your database objects, making it easier to manage and secure your data. This guide will show you how to list all schemas in your PostgreSQL database and change the schema for your session or permanently.

Listing Schemas in PostgreSQL

To list schemas in PostgreSQL, you can use a simple SQL command or the psql command-line tool.

Using SQL Command

You can list all schemas in your PostgreSQL database with the following SQL query:

SELECT schema_name
FROM information_schema.schemata;

This command returns a list of all schemas in the current database.

Using psql Command-Line Tool

If you're using the psql command-line tool, you can list all schemas with the \dn command:

\dn

This command displays all available schemas in your database.

Changing the Current Schema

Sometimes, you may need to change the current schema in your PostgreSQL session. This can be done temporarily for the current session or permanently for a user.

Temporary Change for Current Session

To temporarily change the schema for the current session, use the SET command:

SET search_path TO new_schema;

Replace new_schema with the name of the schema you want to use. This change applies only to the current session.

Permanent Change for a User

To permanently change the default schema for a user, you need to alter the user settings:

ALTER ROLE username SET search_path TO new_schema;

Replace username with the name of the user and new_schema with the desired schema. This change will apply every time the user connects to the database.

Using Multiple Schemas

You can also set multiple schemas in the search path. This allows PostgreSQL to search for objects in the specified order:

SET search_path TO schema1, schema2, public;

In this example, PostgreSQL will first search in schema1, then schema2, and finally in the public schema.

Creating a New Schema

Creating a new schema in PostgreSQL is straightforward. Use the following SQL command:

CREATE SCHEMA new_schema;

Replace new_schema with the name of your new schema. You can now use this schema to organize your database objects.

Granting Permissions to Schemas

After creating a new schema, you may need to grant permissions to other users:

GRANT ALL ON SCHEMA new_schema TO username;

Replace new_schema with the name of your schema and username with the name of the user.

Conclusion

Schemas are a powerful feature in PostgreSQL, helping you manage and organize your database objects efficiently. Knowing how to list schemas and change the current schema can improve your workflow and database management.

For advanced balancing management and high performance, consider using Akmatori. Akmatori is a globally distributed TCP/UDP load balancer that ensures your services remain available and responsive.

Thanks for reading. If you have any questions or need further assistance, feel free to reach out. Happy managing your PostgreSQL databases!

Maximize your website or application's performance and reliability!