PostgreSQL – INSERT INTO Query
To insert a row into PostgreSQL table, you can use INSERT INTO query with the following syntax.
INSERT INTO table_name(
column1, column2,.., columnN)
VALUES (value1, value2,.., valueN);
where
- table_name is the name of table in which you would like to insert the row.
- column1, column2,.., columnN are the columns of the table.
- value1, value2,.., valueN are the respective values of the columns.
Example 1 – INSERT INTO table
In the following example, we are inserting an entry into students
table of mydb
database.
INSERT INTO students(
id, name, age)
VALUES (1, 'Jack', 24);
Under the Messages, we got that INSERT Query returned successfully.
You can verify if the row is inserted to table, by selecting the rows of the table.
Yes, the row is inserted to the table.
Example 2 – INSERT multiple rows into table in a single query
We can also insert multiple rows into PostgreSQL table using a single query. All you have to do is, provide multiple rows after VALUES keywork in the INSERT query as shown below.
INSERT INTO students(
id, name, age)
VALUES (2, 'Wick', 22), (3, 'Tom', 19), (4, 'Jason', 23);
Here we have three rows to be inserted in a single query.
When you run this query, three rows would be inserted into the table.
Conclusion
In this PostgreSQL Tutorial, we learned to insert one or more rows into PostgreSQL table.