PostgreSQL

PostgreSQL INSERT INTO

Insert one or more rows into a PostgreSQL table and verify the saved data.

Before you begin: Connect to the target database in pgAdmin 4 and make sure the table already exists. This guide uses the products table from the Create Table guide.

Insert one row

To add a row to a PostgreSQL table, use INSERT INTO followed by the table name, the columns you want to fill, and a VALUES clause:

INSERT INTO products (name, model, year)
VALUES ('Laptop', 'Pro 14', 2025);

This statement inserts one product into the products table. The first value is stored in name, the second in model, and the third in year. The column order must match the value order. Listing the columns explicitly makes the statement easier to read and protects it from changes to the table's column order.

SQL statement explained

  • INSERT INTO products identifies the table that receives the new row.
  • (name, model, year) identifies the columns that will receive values.
  • VALUES begins the data to insert.
  • ('Laptop', 'Pro 14', 2025) contains the values in the same order as the columns.

Text values must be enclosed in single quotation marks. Numeric values such as 2025 are written without quotation marks. A semicolon marks the end of the SQL statement.

Read the insert result

If you run the statement in the PostgreSQL SQL Shell (psql), it reports:

INSERT 0 1

The final number, 1, means that one row was inserted. The middle number is an older object-identifier field and is normally 0 in modern PostgreSQL tables, so it can be ignored here. In pgAdmin 4, a successful execution is shown in the Query Tool's messages area.

Insert multiple rows

Insert several rows with one statement by separating value groups with commas:

INSERT INTO products (name, model, year)
VALUES
  ('Laptop', 'Pro 14', 2025),
  ('Monitor', 'UltraView 27', 2024),
  ('Keyboard', 'Mechanical K1', 2023);

Verify the inserted data

Use a SELECT query to display the rows in the table:

SELECT * FROM products;

In pgAdmin 4, run the query in the Query Tool and inspect the Data Output panel. After the single insert above, the result will look like this:

  name  |  model  | year
--------+---------+------
 Laptop | Pro 14  | 2025
(1 row)

Return inserted values

When a table has a generated identifier, PostgreSQL can return the inserted row immediately with RETURNING:

INSERT INTO products (name, model, year)
VALUES ('Tablet', 'Air 11', 2025)
RETURNING *;

Insert data best practices

  • List the target columns explicitly instead of relying on table order.
  • Use parameters in application code instead of concatenating user input into SQL.
  • Use a transaction when several inserts must succeed or fail together.
  • Validate values and use constraints such as NOT NULL and UNIQUE.
  • Use RETURNING when the application needs generated values immediately.
Continue learning: After inserting data, practice filtering with WHERE, changing rows with UPDATE, and removing rows with DELETE.

Further Reading

Continue learning with these related tutorials:

Continue learning