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 essentially a namespace that allows users to create and manage database objects (like tables, views, indexes, etc.) under a logical group. Schemas are useful for organizing database objects and can also be used to manage access permissions.
The CREATE SCHEMA
command is used to create a new schema in a database.
CREATE SCHEMA [IF NOT EXISTS] schema_name [AUTHORIZATION owner_name] [schema_element [...]];
IF NOT EXISTS
: Allows you to create the schema only if it doesn't already exist. This is useful to avoid errors in scripts that might try to create a schema multiple times.
schema_name
: The name you want to give to the schema.
AUTHORIZATION owner_name
: Specifies the owner of the schema. If omitted, the current user is the default owner.
schema_element
: You can also include schema elements (like table, view definitions) as part of the schema creation. This is less commonly used in practice.
Basic schema creation:
CREATE SCHEMA myschema;
Create schema with specific owner:
CREATE SCHEMA myschema AUTHORIZATION myuser;
Create schema and define a table within it:
CREATE SCHEMA myschema CREATE TABLE mytable (id INT PRIMARY KEY, name TEXT);
Once you have objects inside a schema, you reference them using the schema's name as a prefix:
SELECT * FROM myschema.mytable;
By default, when a user creates an object without specifying a schema, PostgreSQL will use the public
schema.
You can manage access permissions at the schema level. This allows you to group objects by purpose or by user/team and manage permissions more easily.
Remember to set the search_path
appropriately, especially if you're working with multiple schemas in a database. The search_path
determines in which schemas PostgreSQL should look for objects when an unqualified object name is used. For example:
SET search_path TO myschema,public;
This tells PostgreSQL to look in myschema
first and then in public
when trying to resolve object names.
Always design the schema structure keeping in mind the logical organization of data, future growth, and the security/access needs of different users or teams.
Creating a new schema in PostgreSQL:
CREATE SCHEMA your_schema;
Setting the owner of a schema in PostgreSQL:
CREATE SCHEMA your_schema AUTHORIZATION your_user;
Defining tables within a specific schema in PostgreSQL:
CREATE TABLE your_schema.your_table ( column1 datatype, column2 datatype );
Creating schemas with different character sets and collations in PostgreSQL:
CREATE SCHEMA your_schema AUTHORIZATION your_user ENCODING 'UTF8' LC_COLLATE='en_US.UTF-8' LC_CTYPE='en_US.UTF-8';
Granting permissions on schemas in PostgreSQL:
GRANT USAGE, CREATE ON SCHEMA your_schema TO your_user;
Viewing existing schemas in PostgreSQL:
\dn
Altering schemas with ALTER SCHEMA in PostgreSQL:
ALTER SCHEMA your_schema RENAME TO new_schema; ALTER SCHEMA your_schema OWNER TO new_owner;
Dropping schemas with DROP SCHEMA in PostgreSQL:
DROP SCHEMA your_schema;