Using the psql CLI
psql is the default Postgres command-line tool.
# Connect
psql -U app_user -d app_db
\l -- list databases
\c app_db -- connect to database
\dt -- list tables
\d users -- describe table
\q -- quit
CRUD Basics
-- Create (INSERT)
INSERT INTO users (name, email)
VALUES ('Linus', 'linus@example.com');
-- Read (SELECT)
SELECT id, name, email FROM users
WHERE email LIKE '%@example.com'
ORDER BY created_at DESC
LIMIT 10;
-- Update
UPDATE users
SET name = 'Linus Torvalds'
WHERE email = 'linus@example.com';
-- Delete
DELETE FROM users
WHERE email = 'linus@example.com';
Tip: Always Use WHERE
Forgetting a WHERE clause on UPDATE or DELETE can affect every row.
Many teams use BEGIN and ROLLBACK while experimenting.