The UPDATE statement changes column values in existing rows. You target the table, declare new values with SET, and narrow which rows are affected with WHERE. Omitting WHERE updates every row in the table — always double-check before running an unfiltered update in production.
Basic UPDATE syntax
The standard form updates one or more columns for rows that match the condition:
UPDATE courses
SET published_date = '2020-08-01'
WHERE course_id = 3;
Output:
UPDATE 1
The command tag returns the count of rows updated, including rows where the value did not actually change. To update multiple columns at once, separate each assignment with a comma in the SET clause.
You can also update a column based on its current value using an expression:
-- Increase every course price by 5%
UPDATE courses
SET price = price * 1.05;
Output:
UPDATE 5
Because there is no WHERE clause, all five rows are updated.
Returning the updated rows
PostgreSQL extends standard SQL with a RETURNING clause that returns the updated row values after the change is applied — no additional SELECT required:
UPDATE courses
SET published_date = '2020-07-01'
WHERE course_id = 2
RETURNING *;
Output:
course_id | course_name | price | description | published_date
-----------+----------------------------+--------+----------------------------+----------------
2 | PostgreSQL Admininstration | 349.99 | A PostgreSQL Guide for DBA | 2020-07-01
(1 row)
You can return specific columns or rename them with AS:
UPDATE courses
SET price = price * 1.10
WHERE course_id = 1
RETURNING course_id, course_name, price AS new_price;
Practical tips
- Always test your
WHEREclause with aSELECTfirst to confirm which rows will be affected before running the update. - Wrap updates in an explicit transaction when they are part of a multi-step operation — you can
ROLLBACKif something goes wrong downstream. - To update rows based on data in another table, use the
UPDATE ... FROM(update-join) syntax rather than a correlated subquery. - The
RETURNINGclause reflects values after the update, so it is safe to use for audit logging in the same statement. - UPDATE acquires a row-level lock on every row it touches — for large batch updates, consider updating in smaller chunks to avoid long lock contention.
Reference: PostgreSQL documentation — UPDATE.