This repository contains an overview of basic PostgreSQL commands, installation steps, and common SQL queries. PostgreSQL is a powerful, open-source object-relational database system with a strong reputation for reliability and performance.
- Download and Install PostgreSQL
- Add PostgreSQL to PATH
- Create Shortcuts for pgAdmin and psql Shell
- Basic PostgreSQL Commands
- User and Role Management
- Database Management
- Data Types
- Table Management
- Data Manipulation
- Query Clauses
- Indexes and Constraints
- Transactions
- Backup and Restore
- Advanced Queries
- Visit the official PostgreSQL Downloads page.
- Select your operating system (Windows, macOS, or Linux).
- Follow the installation steps provided by the PostgreSQL installer.
- After installation, locate the PostgreSQL installation directory (usually in
C:\Program Files\PostgreSQL\version\bin
). - Add this path to the system's environment variables:
- Open "System Properties" (Right-click
This PC
→Properties
→Advanced system settings
). - Under "System Properties" →
Advanced
→ ClickEnvironment Variables
. - In the "System Variables" section, find
Path
and clickEdit
. - Add the PostgreSQL
bin
folder to the path, and clickOK
.
- Open "System Properties" (Right-click
-
For pgAdmin (GUI):
- Locate
pgAdmin 4
in your installed programs or the PostgreSQL folder. - Right-click on the program →
Send to
→Desktop (create shortcut)
.
- Locate
-
For psql Shell (CLI):
- Find the
SQL Shell (psql)
in the PostgreSQL directory or Start menu. - Right-click on it →
Send to
→Desktop (create shortcut)
.
- Find the
-
Connect to a Database:
psql -U username -d dbname
Once connected, type
help
for help:You are using psql, the command-line interface to PostgreSQL. Type: \copyright for distribution terms \h for help with SQL commands \? for help with psql commands \g or terminate with semicolon to execute query \q to quit
-
Get help for psql commands before connecting:
psql --help
This displays a list of available options and their descriptions, such as:
-U username
to specify the user.-d dbname
to connect to a specific database.-h host
to connect to a specific host.-p port
to specify the port number.
- Create User:
CREATE USER username WITH PASSWORD 'password';
- Grant Superuser Privileges:
ALTER USER username WITH SUPERUSER;
- Grant User Privileges:
GRANT ALL PRIVILEGES ON DATABASE dbname TO username;
- List Users:
\du \du+ -- for more details
- Delete User:
DROP USER username;
- Create Database:
CREATE DATABASE dbname;
- Switch to Another Database:
\c dbname
- List Databases:
\l
- Delete Database:
DROP DATABASE dbname;
- Execute SQL Script from a File:
\i /path/to/file.sql
Data Type | Description |
---|---|
INTEGER |
Stores whole numbers |
SERIAL |
Auto-incrementing integer |
BIGINT |
Stores large whole numbers |
NUMERIC(p, s) |
Stores exact numbers with precision and scale |
REAL |
Stores floating-point numbers |
DOUBLE PRECISION |
Stores double precision floating-point numbers |
VARCHAR(n) |
Stores variable-length text, up to n characters |
TEXT |
Stores variable-length text (unlimited) |
BOOLEAN |
Stores TRUE or FALSE |
DATE |
Stores a date (year, month, day) |
TIMESTAMP |
Stores both date and time |
UUID |
Stores universally unique identifiers |
- Create a Table with
PRIMARY KEY
andNOT NULL
:CREATE TABLE tablename ( id SERIAL PRIMARY KEY, column1 VARCHAR(255) NOT NULL, column2 INTEGER NOT NULL, column3 DATE );
- View Table Schema:
\d tablename
- Insert Data:
INSERT INTO tablename (column1, column2) VALUES (value1, value2);
- Update Data:
UPDATE tablename SET column1 = value1 WHERE condition;
- Delete Data:
DELETE FROM tablename WHERE condition;
- Select Data:
SELECT column1, column2 FROM tablename WHERE condition;
- Using AND, OR conditions in WHERE:
SELECT * FROM tablename WHERE condition1 AND (condition2 OR condition3);
- LIMIT Clause:
SELECT * FROM tablename LIMIT 10;
- OFFSET Clause:
SELECT * FROM tablename LIMIT 10 OFFSET 5;
- FETCH Clause:
SELECT * FROM tablename OFFSET 5 FETCH NEXT 10 ROWS ONLY;
- IN Clause:
SELECT * FROM tablename WHERE column IN ('value1', 'value2', 'value3');
- DISTINCT Clause:
SELECT DISTINCT column FROM tablename;
- ORDER BY Clause:
SELECT * FROM tablename ORDER BY column ASC; -- Order results in ascending order (default) SELECT * FROM tablename ORDER BY column DESC; -- Order results in descending order:
-
Create Index:
CREATE INDEX indexname ON tablename (columnname);
-
Add Foreign Key:
ALTER TABLE tablename ADD CONSTRAINT fk_name FOREIGN KEY (columnname) REFERENCES other_table (columnname);
-
Begin Transaction:
BEGIN;
-
Commit Transaction:
COMMIT;
-
Rollback Transaction:
ROLLBACK;
- Backup a Database:
pg_dump dbname > backupfile.sql
- Backup only the schema (pre-data including indexes, sequences, etc.):
pg_dump --schema-only dbname > schema_backup.sql
- Restore a Database:
psql dbname < backupfile.sql
-
Join Tables:
SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column;
-
Group By:
SELECT column, COUNT(*) FROM tablename GROUP BY column;