PostgreSQL is an advanced, open-source Object-Relational Database (ORDBMS) renowned for its stability, extensibility (supporting features like user-defined types and table inheritance), and strict adherence to the SQL standard. This cheat sheet provides a quick reference for fundamental DDL (Data Definition Language) and DML (Data Manipulation Language) commands used for managing core PostgreSQL objects.
I. PostgreSQL Database Management (DDL)
These commands operate at the highest hierarchical level, managing entire database instances. They are typically executed via the psql client or directly in SQL.
Database Commands
| Action | Syntax |
|---|---|
| Create Database |
|
| Drop Database |
|
| Access/Connect (psql) |
|
| List All Databases (psql) |
|
| Rename Database |
|
| Change Database Owner |
|
| Alter Configuration |
|
II. PostgreSQL Schema Management
Schemas are namespaces within a database, used to organize objects (tables, functions, etc.) and prevent naming conflicts. They simplify access control and logical grouping of objects.
Schema Commands
| Action | Syntax |
|---|---|
| Create Schema |
|
| Drop Schema |
|
| Rename Schema |
|
| Change Schema Owner |
|
| Show All Schemas (psql) |
|
III. PostgreSQL Table Management (DDL & DML)
These commands manage the definition (DDL) and manipulation (DML) of table structures and data.
Table DDL & DML Commands
| Action | Syntax |
|---|---|
| Create Table |
|
| Drop Table |
|
| Rename Table |
|
| Add Column |
|
| Drop Column |
|
| Rename Column |
|
| Fetch All Data (DML) |
|
| Insert Data (DML) |
|
| Delete Specific Records (DML) |
|
| List Tables (psql) |
|
IV. PostgreSQL Views and Indexes
Views simplify complex queries and provide a layer of abstraction, while Indexes speed up data retrieval performance.
Views and Indexes Commands
| Action | Syntax |
|---|---|
| Create Standard View |
|
| Create Materialized View |
|
| Refresh Materialized View |
|
| Drop Standard View |
|
| Create Index |
|
| Drop Index |
|
V. PostgreSQL User and Role Management (DCL)
Roles and Users are crucial for managing database access control and privileges (Data Control Language).
User and Role Commands
| Action | Syntax |
|---|---|
| Create User/Role |
|
| Create Superuser |
|
| Drop User |
|
| Rename User |
|
| Change Password/Options |
|
| Find User Details (SQL) |
|
| List Users (psql) |
|
Frequently Asked Questions (FAQ) on PostgreSQL Commands
Q: What is the difference between CASCADE and RESTRICT when dropping objects?
A: The CASCADE option automatically drops all objects that depend on the object being dropped (e.g., dropping a schema with CASCADE will drop all tables in it). The RESTRICT option (default behavior) prevents the drop operation if any other object depends on it, forcing you to manually remove dependencies first.
Q: What are psql meta-commands (like \c or \dt)?
A: Meta-commands (starting with a backslash \) are commands executed within the psql interactive terminal, not sent directly to the PostgreSQL server as SQL. They are used for administrative functions like connecting to a database (\c), listing tables (\dt), or showing user permissions (\du).
Q: Why should I use a Materialized View instead of a regular View?
A: A Materialized View (MV) stores the query results physically on disk, making subsequent reads faster because the query doesn't need to be re-executed. A regular View runs the underlying query every time it is accessed. MVs are ideal for reporting or slow, complex queries where data can be slightly stale, requiring manual or scheduled refresh (using REFRESH MATERIALIZED VIEW).