PostgreSQL, a powerful open-source relational database, is widely used for its robustness and flexibility. Whether you're managing databases, querying data, or handling user permissions, mastering its core commands is essential. This article outlines the most frequently used PostgreSQL commands, grouped by function, with practical examples to help you get started. These commands are executed via the psql command-line interface or GUI tools like pgAdmin.
Creates a new database.
- Syntax: CREATE DATABASE dbname;
- Example:
- Creates a database named company.
Deletes a database and its contents.
- Syntax: DROP DATABASE dbname;
- Example:
- Removes the company database (use with caution).
Switches to a specific database in psql.
- Syntax: \c dbname
- Example:
- Connects to the company database.
Defines a new table with columns and constraints.
- Syntax:
- Example:
- Creates an employees table with an auto-incrementing id, required name, salary, and hire_date.
Deletes a table and its data.
- Syntax: DROP TABLE tablename;
- Example:
- Removes the employees table.
Modifies a table’s structure.
- Syntax: ALTER TABLE tablename ADD column_name datatype;
- Example:
- Adds a department column to employees.
Adds new rows to a table.
- Syntax:
- Example:
- Inserts a new employee record.
Modifies existing rows.
- Syntax:
- Example:
- Updates Alice’s salary to 80,000.
Removes rows based on a condition.
- Syntax:
- Example:
- Deletes all Engineering department employees.
Retrieves specific columns.
- Syntax:
- Example:
- Fetches names and salaries of employees earning over 70,000.
Retrieves all columns.
- Syntax: SELECT * FROM tablename;
- Example:
- Returns all data from employees.
Combines data from multiple tables.
- Syntax:
- Example:
- Shows employee names and their department names.
Groups rows for aggregation.
- Syntax:
- Example:
- Calculates the average salary per department.
Improves query performance.
- Syntax: CREATE INDEX index_name ON tablename (column_name);
- Example:
- Indexes the name column for faster searches.
Removes an index.
- Syntax: DROP INDEX index_name;
- Example:
- Deletes the name index.
Creates a new database user.
- Syntax: CREATE USER username WITH PASSWORD 'password';
- Example:
- Creates a user named analyst.
Assigns permissions to a user.
- Syntax: GRANT privilege_name ON tablename TO username;
- Example:
- Allows analyst to read and insert into employees.
Removes user permissions.
- Syntax: REVOKE privilege_name ON tablename FROM username;
- Example:
- Removes analyst’s insert permission.
Shows all databases.
- Syntax: \l
- Example:
- Lists databases like company and postgres.
Lists tables in the current database.
- Syntax: \dt
- Example:
- Shows tables like employees and departments.
Displays a table’s structure.
- Syntax: \d tablename
- Example:
- Shows columns, types, and constraints of employees.
Runs commands from a .sql file.
- Syntax: \i filename.sql
- Example:
- Executes setup.sql to create tables or insert data.
Exits the psql interface.
- Syntax: \q
- Example:
- Closes the psql session.
Starts a transaction block.
- Syntax: BEGIN;
- Example:
- Starts a transaction for inserting a row.
Saves transaction changes.
- Syntax: COMMIT;
- Example:
- Permanently saves the new employee.
Discards transaction changes.
- Syntax: ROLLBACK;
- Example:
- Discards the insertion.
Counts rows.
- Syntax: SELECT COUNT(*) FROM tablename;
- Example:
- Counts Engineering employees.
Calculates a column’s total.
- Syntax: SELECT SUM(column) FROM tablename;
- Example:
- Sums all salaries.
Computes a column’s average.
- Syntax: SELECT AVG(column) FROM tablename;
- Example:
- Averages Engineering salaries.
These 29 PostgreSQL commands cover the essentials for managing databases, tables, data, and users. From creating and querying tables to handling transactions and permissions, they form the backbone of most PostgreSQL workflows. Commands like SELECT, INSERT, and GROUP BY are daily staples for developers, while psql utilities like \dt and \l streamline administration. For advanced features like triggers or window functions, consult PostgreSQL’s documentation or experiment with these basics first. Start practicing in a test environment to build confidence!