PostgreSQL Update
The UPDATE statement lets you modify the values of existing rows in a table, either across every row or only the ones that match a condition.
The UPDATE Statement
UPDATE changes the values of one or more columns in existing rows. The basic form is UPDATE table_name SET column1 = value1, column2 = value2 WHERE condition. The WHERE clause determines which rows are affected - leave it out and every row in the table is updated, so it deserves the same care you'd give a DELETE statement.
Update a single column
UPDATE employees
SET salary = 65000
WHERE employee_id = 104;Updating Multiple Columns at Once
You can update several columns in a single statement by separating each assignment with a comma. PostgreSQL evaluates every expression on the right-hand side using the row's values before the update runs, so you can safely reference a column's current value, such as salary = salary * 1.05, in the same statement.
Update multiple columns together
UPDATE employees
SET department = 'Marketing',
salary = salary * 1.05
WHERE department = 'Sales';Updating Rows Using Another Table (UPDATE ... FROM)
Sometimes the new value for a row depends on data in a different table. PostgreSQL's UPDATE ... FROM clause lets you join another table into the update, and RETURNING lets you see exactly which rows changed and what their new values are, without running a separate SELECT afterward.
Update using a joined table
UPDATE orders o
SET status = 'shipped'
FROM shipments s
WHERE o.order_id = s.order_id
AND s.shipped_date IS NOT NULL
RETURNING o.order_id, o.status;- Run the WHERE clause as a SELECT first to preview affected rows.
- Wrap risky updates in a transaction (BEGIN ... COMMIT) so you can roll back if something looks wrong.
- Add RETURNING to confirm exactly which rows and values changed.
- Index the columns used in your WHERE clause so large updates run efficiently.
- Avoid updating primary key or foreign key columns directly; consider whether related rows need to change too.
Exercise: PostgreSQL Update
What happens if you run an UPDATE statement without a WHERE clause?