<< back to Guides
๐น Accessing
<< back to Guides
๐ข๏ธ PostgreSQL Essentials: CLI, Configuration, Users, and SQL
This guide covers day-to-day PostgreSQL usage, including CLI access, configuration, user/role management, SQL syntax, and admin tasks โ ideal for server-side environments and SRE workflows.
๐ 1. Using the PostgreSQL Client
๐น Accessing psql
# Connect as default user
psql
# Connect to specific DB
psql -U postgres -d mydb -h 127.0.0.1 -p 5432
Use
\q
to quit the shell, and\?
for help.
๐น Basic psql Commands
\l -- list databases
\c mydb -- connect to database
\dt -- list tables
\d users -- describe table schema
\du -- list roles
โ๏ธ 2. PostgreSQL Configuration
๐น Config File Locations
File | Purpose |
---|---|
/etc/postgresql/14/main/postgresql.conf |
Main settings |
/etc/postgresql/14/main/pg_hba.conf |
Client access rules |
/etc/postgresql/14/main/pg_ident.conf |
User mapping rules |
On Alpine or Docker, you may find them under
/var/lib/postgresql/data/
.
๐น Key Config Parameters
In postgresql.conf
:
listen_addresses = '*'
port = 5432
max_connections = 100
shared_buffers = 256MB
log_min_duration_statement = 500 # slow query log
In pg_hba.conf
:
# Allow remote access (host-based auth)
host all all 0.0.0.0/0 md5
Apply config changes with:
sudo systemctl restart postgresql
๐ค 3. Managing Roles and Access
๐น Creating Users (Roles)
CREATE ROLE devuser WITH LOGIN PASSWORD 's3cret';
๐น Creating Databases
CREATE DATABASE myappdb OWNER devuser;
๐น Granting Privileges
GRANT ALL PRIVILEGES ON DATABASE myappdb TO devuser;
\c myappdb
GRANT SELECT, INSERT ON ALL TABLES IN SCHEMA public TO devuser;
๐น Dropping Roles/DBs
DROP DATABASE myappdb;
DROP ROLE devuser;
๐ง 4. PostgreSQL SQL Basics
๐น Data Types
INTEGER
,SERIAL
,TEXT
,VARCHAR
,BOOLEAN
,JSONB
,TIMESTAMP
- Use
UUID
andJSONB
for modern systems
๐น Creating Tables
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE,
metadata JSONB,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
๐น Common Queries
SELECT * FROM users WHERE email = 'john@example.com';
INSERT INTO users (name, email) VALUES ('Jane', 'jane@example.com');
UPDATE users SET name = 'Janet' WHERE id = 1;
DELETE FROM users WHERE id = 1;
๐น Joins and Aggregations
SELECT u.name, o.total
FROM users u
JOIN orders o ON u.id = o.user_id
WHERE o.total > 100;
๐งช 5. Admin & Performance
๐น Backups and Restores
# Backup
pg_dump -U postgres mydb > backup.sql
# Restore
psql -U postgres mydb < backup.sql
Use pg_dumpall
for backing up all roles and DBs.
๐น Analyze Queries
EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'a@b.com';
๐น Tune Performance
Edit postgresql.conf
:
work_mem = 4MB
maintenance_work_mem = 64MB
effective_cache_size = 1GB
Use pgtune
to auto-suggest config values based on system memory.
๐ก๏ธ 6. PostgreSQL Security Tips
- Never expose port 5432 directly to the internet
- Always enforce
md5
orscram-sha-256
auth inpg_hba.conf
- Use
ssl = on
for encrypted traffic - Backup
pg_hba.conf
andpg_ident.conf
๐งฐ Useful psql Tips
-- Output query results as aligned table
\x
-- Export query to CSV
\COPY (SELECT * FROM users) TO 'users.csv' CSV HEADER;
-- Change prompt
\set PROMPT1 '%n@%/%R%# '
๐ Resources
โ Summary
Task | Command |
---|---|
Connect to DB | psql -U user -d db |
Create Role/DB | CREATE ROLE , CREATE DATABASE |
Grant Permissions | GRANT SELECT ON ... TO user |
Backup | pg_dump , pg_dumpall |
Query Analysis | EXPLAIN ANALYZE ... |
Configure Access | pg_hba.conf , postgresql.conf |