POSTGRESQL

The Postgres UPDATE Statement: A Deep Dive

intro

Let’s take a look at the essentials of using the Postgres UPDATE statement to update data in one or more columns of one or more rows in a table.

In this technical article, we will look into the Postgres UPDATE statement, and understand its statement syntax. From updating single and multiple rows to incorporating conditions, and expressions, we will learn the ins and outs behind data modification in PostgreSQL.

Time to get started!

How to Use the UPDATE Postgres Statement

Here’s the basic syntax of the Postgres UPDATE statement:

Copy
        
1 UPDATE table_name 2 SET column1 = value1, 3 column2 = value2, 4 ... 5 WHERE condition;

Let’s break the building blocks of this syntax:

  1. Specify the name of the table that you’re looking forward to updating the data after the UPDATE keyword.
  2. Add the columns and their new values after SET keyword. The columns that do not appear in the SET clause will retain their original values.
  3. Determine which rows to update in the condition of the WHERE clause. The WHERE clause is a conditional clause that determines which rows will be affected by the update.

Before we go into understanding the logic behind the implementation of the UPDATE statement in Postgres, it is important for us to note that:

  • The WHERE clause is optional and if you omit the WHERE clause, the UPDATE statement will update all rows in the table.
  • The UPDATE statement has an optional RETURNING clause (RETURNING *;) that returns the updated rows.

Postgres UPDATE: Real-World Examples

Time to explore some examples of Postgres UPDATE. Set up a demo table by running the query below:

Copy
        
1 CREATE TABLE dbs ( 2 db_id SERIAL PRIMARY KEY, 3 db_name VARCHAR(255) NOT NULL, 4 years_active INT NOT NULL, 5 description VARCHAR(500) 6 ); 7 8 INSERT INTO dbs (db_name, years_active, description) 9 VALUES 10 ('PostgreSQL', 12, 'A complete PostgreSQL for Developers'), 11 ('MySQL', 30, 'MySQL Guide for DBA'), 12 ('MariaDB', 9, NULL), 13 ('Cassandra', 20, 'Cassandra Beginners'); 14 15 SELECT * FROM dbs;

Executing in DbVisualizer, a full-feature database client that supports PostgreSQL and many other databases:

Setting up a demo table
Setting up a demo table

Setting up a demo table

Example #1: Update a Single Record With a New Value

Consider a scenario where we want to update the active years of a database to match the current year. The following statement uses the UPDATE statement to update the db with id 3 by changing the years_active to 10.

Copy
        
1 UPDATE dbs 2 SET years_active = '10' 3 WHERE db_id = 3;

Run the query above in DbVisualizer and you will receive a successful execution message:

Successful execution of the update statement in DbVisualizer
Successful execution of the update statement in DbVisualizer

By exploring the “Data” tab, you can verify that the update has taken place:

Updated the record with id 3 by changing the years_active to 10.
Updated the record with id 3 by changing the years_active to 10.

Example #2: Update All Records

Consider another Postgres update scenario where you want to change the value of a column in al records of a table. The following statement uses an UPDATE statement to increase the years_active of all the courses by 2:

Copy
        
1 UPDATE dbs 2 SET years_active = years_active + 2;

Because we did not use a WHERE clause, the UPDATE statement updates all the rows in the dbs table.

Query build successful in DbVisualizer
Query build successful in DbVisualizer

Query build successful in DbVisualizer

DbVisualizer shows the affected changes:

years_active updated as desired
years_active updated as desired

Best Practices and Tips for Using the Postgres UPDATE Statement

Below are some of the best practices that should be taken into consideration when working with the UPDATE statement in Postgres:

  • Clearly define which rows you want to modify using the WHERE clause. Avoid overly broad updates that might affect unintended data.
  • Test the UPDATE statement on a copy of the table or a development environment before applying updates to your main table. This helps ensure the update behaves as expected and avoids accidental modifications to production data.
  • Simplify the WHERE conditions to optimize performance. Overly complex WHERE clauses can slow down updates.

It’s a wrap!

Conclusion

The Postgres UPDATE statement provides a robust method for updating data in one or more columns of one or more rows in a table. To better appreciate its capabilities, you need a tool that helps you manage databases and visually explore query results. This is where a full-featured database client like DbVisualizer comes in. In addition to being able to connect to several DBMSs, it offers advanced query optimization functionality, and full support for all database features, including UPDATE operations. Download DbVisualizer for free now!

FAQ

What is the UPDATE statement in PostgreSQL?

The UPDATE statement in PostgreSQL allows you to modify existing data in one or more columns of a table. You can update specific rows based on a condition or modify all rows in the table.

What is the basic syntax for the UPDATE Postgres command?

The basic syntax for the UPDATE Postgres statement is:

Copy
        
1 UPDATE table_name 2 SET column1 = value1, 3 column2 = value2, 4 ... 5 WHERE condition;

How can I update multiple columns with a single query in Postgres?

You can specify multiple comma-separated column-value pairs in the SET clause to update several columns at once like this:

Copy
        
1 UPDATE table_name 2 SET column1 = value1, 3 column2 = value2, 4 column3 = value3, 5 ... 6 WHERE condition;

How do I update all rows in a PostgreSQL table?

Omit the WHERE clause in a Postgres UPDATE query to update every row in the table. To avoid accidentally updating all data, keep in mind that it is generally safer to use a WHERE clause.

Dbvis download link img
About the author
Leslie S. Gyamfi.
Leslie S. Gyamfi
Leslie Gyamfi is a mobile/web app developer with a passion for creating innovative solutions. He is dedicated to delivering high-quality products and technical articles. You can connect with him on LinkedIn
The Table Icon
Sign up to receive The Table's roundup
More from the table
Title Author Tags Length Published
title

SQL Upsert: Inserting a Record If It Does Not Exist

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-12-10
title

How to Find Elements In an Array: PostgreSQL Guide

author Antonello Zanini tags POSTGRESQL 3 min 2024-12-05
title

How and Why to Add an Index in SQL

author Lukas Vileikis tags MySQL POSTGRESQL Search SQL SQL SERVER 8 min 2024-11-28
title

Postgres Create Array of Numbers: Complete Guide

author Lukas Vileikis tags POSTGRESQL SQL 7 min 2024-11-21
title

List of SQL Reserved Words in All Major Databases

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 16 min 2024-11-20
title

SQL Variable: What It Is and How To Use It

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 8 min 2024-11-18
title

Postgres DATEDIFF Equivalent Functions: Calculating Date and Time Differences

author Lukas Vileikis tags POSTGRESQL 6 min 2024-11-04
title

CREATE SCHEMA PostgreSQL Statement: What, Why & When to Use

author Lukas Vileikis tags POSTGRESQL 4 min 2024-10-30
title

SQL EXPLAIN: The Definitive Tool to Optimize Queries

author Antonello Zanini tags MySQL POSTGRESQL SQL 12 min 2024-10-29
title

SQL Not Equal Operator: Definitive Guide with Examples

author Antonello Zanini tags MySQL ORACLE POSTGRESQL SQL SQL SERVER 6 min 2024-10-14

The content provided on dbvis.com/thetable, including but not limited to code and examples, is intended for educational and informational purposes only. We do not make any warranties or representations of any kind. Read more here.

Cookie policy

We use cookies to ensure that we give you the best experience on our website. However you can change your cookie settings at any time in your browser settings. Please find our cookie policy here ↗