📜  PostgreSQL – ALTER SCHEMA(1)

📅  最后修改于: 2023-12-03 15:03:49.233000             🧑  作者: Mango

PostgreSQL – ALTER SCHEMA

PostgreSQL allows you to modify existing schemas in your database using the ALTER SCHEMA command. This command can be used to perform a variety of tasks, ranging from simply renaming an existing schema to changing its owner or setting default privileges.

Renaming a Schema

To rename an existing schema in PostgreSQL, use the ALTER SCHEMA command along with the RENAME TO option. The following example demonstrates how to rename a schema called old_schema to new_schema.

ALTER SCHEMA old_schema RENAME TO new_schema;
Changing the Owner of a Schema

You can change the owner of an existing schema in PostgreSQL using the ALTER SCHEMA command with the OWNER TO option. The following example demonstrates how to change the owner of a schema called my_schema to a user named new_owner.

ALTER SCHEMA my_schema OWNER TO new_owner;
Setting Default Privileges

By default, when a new object is created within a schema, it inherits the privileges of the schema. You can modify these privileges by setting default privileges for the schema, which can be done using the ALTER DEFAULT PRIVILEGES command. The following example demonstrates how to grant all privileges on tables created in a schema called my_schema to the user my_user.

ALTER DEFAULT PRIVILEGES IN SCHEMA my_schema GRANT ALL ON TABLES TO my_user;
Conclusion

The ALTER SCHEMA command in PostgreSQL provides you with a range of options for modifying existing schemas in your database. Whether you need to rename a schema, change its owner, or set default privileges, this command makes it easy to do.