PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, a schema is a namespace that contains named database objects such as tables, views, indexes, data types, functions, and operators. If you want to remove a schema, you can use the DROP SCHEMA
command.
DROP SCHEMA [IF EXISTS] schema_name [CASCADE | RESTRICT];
IF EXISTS
: An optional keyword to prevent an error if the schema doesn't exist. If you omit this clause and the schema does not exist, an error will be raised.
schema_name
: The name of the schema you want to drop.
CASCADE
: This option will automatically drop objects (tables, views, etc.) within the schema and potentially other dependent objects outside of the schema.
RESTRICT
: This is the default behavior. It prevents the schema from being dropped if there are any objects inside it.
Drop a Simple Schema:
To drop a schema named myschema
:
DROP SCHEMA myschema;
This command will fail if the schema is not empty.
Using CASCADE:
If you want to drop the schema and all of its contained objects:
DROP SCHEMA myschema CASCADE;
Using IF EXISTS:
If you're unsure whether a schema exists and want to avoid an error in case it doesn't:
DROP SCHEMA IF EXISTS myschema;
Loss of Data: Using the CASCADE
option will drop all objects inside the schema, which might result in a loss of data if there are tables within the schema.
Dependencies: Dropping a schema will remove all objects inside it. If other objects in the database depend on objects within the schema, using CASCADE
will drop those dependent objects as well.
Permissions: Only the owner of the schema or a superuser can drop a schema.
Backup: Always backup your database before making significant structural changes, such as dropping schemas.
Dropping a schema is a significant operation that can impact the structure of your database and the availability of data. Always ensure you fully understand the consequences and have taken necessary precautions before executing such operations.
Removing a schema in PostgreSQL:
DROP SCHEMA
to remove a schema.DROP SCHEMA your_schema_name;
Dropping schemas with specific objects in PostgreSQL:
-- Drop or move objects first DROP TABLE your_schema_name.your_table_name; -- Repeat for other objects (e.g., functions, views) -- Drop the schema DROP SCHEMA your_schema_name;
Handling dependencies when dropping schemas in PostgreSQL:
-- Drop dependent objects first DROP VIEW your_schema_name.your_view_name; -- Drop the schema DROP SCHEMA your_schema_name;
Using DROP SCHEMA IF EXISTS in PostgreSQL:
DROP SCHEMA IF EXISTS your_schema_name;
Cascading options with DROP SCHEMA in PostgreSQL:
CASCADE
to automatically drop dependent objects.DROP SCHEMA your_schema_name CASCADE;
Dropping schemas with multiple users in PostgreSQL:
-- Revoke privileges from users first REVOKE ALL PRIVILEGES ON SCHEMA your_schema_name FROM your_user_name; -- Drop the schema DROP SCHEMA your_schema_name;
Dropping built-in schemas in PostgreSQL:
ALTER SCHEMA pg_catalog.your_builtin_schema OWNER TO your_new_owner;
Recovering space after dropping schemas in PostgreSQL:
VACUUM
command.-- Full vacuum on the entire PostgreSQL instance VACUUM FULL; -- Analyze for statistics update ANALYZE;