Skip to content
Related Articles

Related Articles

PostgreSQL – Insert Multiple Values in Various Rows
  • Last Updated : 01 Feb, 2021

PostgreSQL is a type of relational database (RDBMS) similar to MySQL. Relational Database stores data in the form of a table in which each row is a record and each column is one attribute. In this article, we will look into the process of inserting multiple rows in a PostgreSQL database. In general, we use the INSERT statement to insert values in a database. Just an improvised version of the same statement can be used to insert multiple values in various rows of a database as shown in the syntax below:

Syntax :

##When specifying Column Names
Insert into tableName (col1, col2) values (value,value),(value,value),(value,value);

## when not specifying Column Names
Insert into tableName  values (value,value),(value,value),(value,value);

Approach :

  • Our database name is geeksforgeeks and the table name is gfg at the beginning there is no data inside the table. For selecting the database we will use query  \c databaseName.
  • For checking the data inside the table we will use query select *from tableName.
  • Now for inserting values, we will use the table name and the column names and the records to be inserted will be separated by comma(“‘”).
  • The query will be Insert into tableName (col1, col2) values (value,value),(value,value),(value,value).
  • If you want to insert without the column names then the query will be a different one than the above query.
  • Insert into tableName  values (value,value),(value,value),(value,value) . But one thing should be remembered here that in this case, the order of the values being inserted should be the same as that of in the database and all the mandatory columns must be inserted.
  • Now again we will check for records inside the table using select*from tableName.

Example 1 :

In this example, we first selected the database using the \c geeksforgeeks command, and then we looked into the database table records then we inserted the multiples record inside the table then again look inside the database tables. The steps followed are to select the database and then look into the table records then insert multiple values and then again look into the table records.



Example 2 :

Here in this example, we will insert the rows without using the column names but one thing to note about this approach is if you are not giving the column names the order of values being inserted will be the same as that of in the table and all the mandatory columns must be filled in the table otherwise there will be an exception. After inserting the data we will again look into the database table.

My Personal Notes arrow_drop_up
Recommended Articles
Page :