PostgreSQL INSERT: Inserting Data into a Table

⚡ Smart Summary

PostgreSQL INSERT statement adds new rows to a table using SQL. Lessons cover syntax, single and multi-row inserts, the RETURNING clause, default values, and adding records through the pgAdmin interface visually.

  • 📥 Basic Syntax: INSERT INTO table (cols) VALUES (vals) — canonical form.
  • 📋 Multi-Row: Comma-separate value tuples to insert many rows.
  • 🔄 RETURNING: Fetches inserted rows in the same call.
  • 💡 Defaults: Omitted columns receive DEFAULT or NULL.
  • 🖥️ pgAdmin GUI: Add rows visually without writing SQL.
  • 🤖 AI SQL: Copilot generates parameterised INSERTs.

PostgreSQL INSERT

PostgreSQL INSERT statement

In PostgreSQL, the insert statement helps insert a new row or row into the table. You can insert rows specified by value expressions, zero, or multiple rows resulting from the query.

Syntax of PostgreSQL INSERT INTO

INSERT INTO TABLE_NAME (column1, column2, ...columnN)
VALUES (value1, value2,...valueN);

You can see in the above syntax column 1 to N are the names of the columns in the table into which you wanted to insert data. The target column needs to list in any specific order. The values supplied by the query or values clause is either the corresponding values for the columns.

Once the query is executed, you can see the output message.

Insert oid 1

This output message will be displayed if only a single row is inserted, oid is the numeric OID assigned to the inserted row.

Example of PostgreSQL Insert into Table

Consider the following table, “tutorials,” with two columns.

“id” integer DEFAULT value 1
“tutorial_name” text DEFAULT value postgre
And no constraints

PostgreSQL Insert into Table

Here, are steps for PostgreSQL insert into table:

Step 1) Inserting a row into

INSERT INTO tutorials(id, tutorial_name) VALUES (1, 'postgre');

PostgreSQL Insert into Table

NOTE: Only the characters or date values need to be enclosed with single quotes when inserting a row.

Step 2) However, If you insert data into all the columns, you can omit the column names. The same insert statement can also be written as,

INSERT INTO tutorials VALUES (1, 'postgre');

PostgreSQL Insert into Table

Step 3) The data values are listed in the order as the columns appear in the table, separated by commas.

The above syntax has an issue which you need to know the order of the columns in the table. To overcome this problem, you can also list the columns explicitly.

For example, both below-given commands have the same effect as displayed below:

INSERT INTO tutorials(id, tutorial_name) VALUES (1, 'postgre');
INSERT INTO tutorials(tutorial_name, id) VALUES ('postgre',1);

PostgreSQL Insert into Table

Step 4) In this example, you can see that if you do not have values for all the columns, you can omit some of them.

In that case, the columns will be automatically filled with their default values if specified.

INSERT INTO tutorials(id) VALUES (5);

PostgreSQL Insert into Table

Step 5) You can also request default values for individual columns or the entire row:

INSERT INTO tutorials(id, tutorial_name) VALUES (1, DEFAULT);
INSERT INTO tutorials DEFAULT VALUES;

PostgreSQL Insert into Table

Step 6) You can multiple rows with the just single command:

INSERT INTO tutorials(id, tutorial_name) VALUES
(1, 'postgre'),
(2, 'oracle'),
(3, 'mysql'),
(4, 'mongo');

PostgreSQL Insert into Table

Step 7) In PostgreSQL, it is also to insert the result of a query which might be no rows, one row, or multiple rows:

INSERT INTO tutorials (id, tutorial_name)
SELECT id, tutorial_name FROM tutorials
WHERE tutorial_name = 'mysql';

PostgreSQL Insert into Table

PostgreSQL Insert statement using pgAdmin

Here, are steps to Insert statement using pgAdmin in Postgre SQL

Step 1) In the object tree

  1. Right Click on the table where you want to insert data
  2. Select Scripts
  3. INSERT Script

PostgreSQL Insert statement using pgAdmin

Step 2) In the Insert Panel

  1. Edit the Query
  2. Click the lighting button
  3. Observe the output

PostgreSQL Insert statement using pgAdmin

Step 3) You can also goto to Tools > Query Tools to open the Query Editor, but you will not get the default insert query.

PostgreSQL Insert statement using pgAdmin

FAQs

Use a single INSERT with comma-separated tuples: INSERT INTO t (a,b) VALUES (1,2), (3,4). Far faster than multiple single-row INSERTs.

AI tools generate parameterised INSERTs to prevent SQL injection, suggest RETURNING clauses, and convert spreadsheet data into multi-row INSERTs.

Yes. AI produces realistic seed data, infers column types from schemas, and writes Python or shell scripts to batch INSERTs.

RETURNING fetches columns from the rows just inserted in the same statement. Use RETURNING * for all columns or list specific columns.

PostgreSQL has no hard row limit, but very large statements can hit memory limits. Use COPY for bulk loads.

Always use parameterised queries with $1, $2 placeholders. Never concatenate user input into INSERT SQL strings.

Yes. Open the table, choose View/Edit Data, add a new row in the grid. pgAdmin generates the INSERT statement for you.

INSERT adds row-by-row through SQL. COPY streams bulk data from files much faster. Use COPY for initial loads.

Summarize this post with: