Build the mental model
CRUD means create, read, update, and delete. PostgreSQL's `RETURNING` clause returns rows changed by INSERT, UPDATE, or DELETE without an extra SELECT. An UPDATE or DELETE without `WHERE` can affect the entire table, so develop the habit of using transactions and checking affected row counts.
Connect it to a real scenario
Insert two users and update one display name. Instead of guessing the generated identity, obtain it with `RETURNING user_id`. Before deleting, verify the filter with SELECT and consider whether the domain actually requires soft deletion.
Try the working example
INSERT INTO app.users (email, display_name)
VALUES
('a@example.com', 'Aye Aye'),
('b@example.com', 'Bo Bo')
RETURNING user_id, email;
UPDATE app.users
SET display_name = 'Aye Thiri'
WHERE email = 'a@example.com'
RETURNING *;
DELETE FROM app.users
WHERE email = 'b@example.com'
RETURNING user_id;Each insert, update, and delete returns the row it affected.5-minute try-it
Insert three tutorials, then publish one unpublished row and set `published_at = now()`.
One important caution
Accidental UPDATE/DELETE without WHERE is a common data incident. Never experiment against production without a transaction and recoverable backups.
PostgreSQL — Data Manipulation — PostgreSQL Global Development Group