
- PostgreSQL - Home
- PostgreSQL - Overview
- PostgreSQL - Environment Setup
- PostgreSQL - Syntax
- PostgreSQL - Data Types
- PostgreSQL - Operators
- PostgreSQL - Expressions
- PostgreSQL Database
- PostgreSQL - Create Database
- PostgreSQL - ALTER DATABASE
- PostgreSQL - Drop Database
- PostgreSQL - Loading Database
- PostgreSQL - Rename Database
- PostgreSQL - Select Database
- PostgreSQL - Show Database
- PostgreSQL Query Operations
- PostgreSQL - SELECT
- PostgreSQL - CREATE
- PostgreSQL - INSERT
- PostgreSQL - UPDATE
- PostgreSQL - DELETE
- PostgreSQL - ALTER TABLE Command
- PostgreSQL - WHERE Clause
- PostgreSQL - ORDER BY Clause
- PostgreSQL - GROUP BY
- PostgreSQL - HAVING Clause
- PostgreSQL - DISTINCT Keyword
- PostgreSQL - LIMIT Clause
- PostgreSQL - LIKE Clause
- PostgreSQL - WITH Clause
- PostgreSQL - AND & OR Clauses
- PostgreSQL - DROP TABLE
- PostgreSQL - Upsert
- TRUNCATE TABLE Command
- PostgreSQL JOINS & Schemas
- PostgreSQL Schemas
- PostgreSQL Joins
- PostgreSQL Data Integrity & Transaction
- PostgreSQL - Constraints
- PostgreSQL - Transactions
- PostgreSQL - Commit
- PostgreSQL - Rollback
- PostgreSQL - Views
- PostgreSQL Functions
- PostgreSQL - ALIAS Syntax
- PostgreSQL - Functions
- PostgreSQL - Useful Function
- PostgreSQL - MAX() Function
- PostgreSQL - MIN() Function
- PostgreSQL - SUM() Function
- PostgreSQL - COUNT() Function
- PostgreSQL - Array Function
- PostgreSQL - String Function
- PostgreSQL - Numeric Function
- PostgreSQL Operators
- PostgreSQL - UNION Operator
- PostgreSQL - INTERSECT Operator
- PostgreSQL - EXCEPT Operator
- PostgreSQL - ANY Operator
- PostgreSQL - ALL Operator
- PostgreSQL - EXISTS Operator
- PostgreSQL Interface
- PostgreSQL - C / C++
- PostgreSQL - Java
- PostgreSQL - PHP
- PostgreSQL - Perl
- PostgreSQL - Python
- Advanced PostgreSQL
- PostgreSQL - NULL Values
- PostgreSQL - Triggers
- PostgreSQL - Indexes
- PostgreSQL - Locks
- PostgreSQL - Sub Queries
- PostgreSQL - Auto Increment
- PostgreSQL - Privileges
- PostgreSQL - Date/Time Functions & Operators
- PostgreSQL - Errors & Messages
- PostgreSQL - Assert
PostgreSQL - INSERT
The PostgreSQL INSERT INTO statement allows user to insert one or new rows into a table. One can insert a single row at a time or several rows as a result of a query.
Syntax
Following is the basic syntax of INSERT INTO statement in PostgreSQL −
INSERT INTO TABLE_NAME (column1, column2, column3, ..., columnN) VALUES (value1, value2, value3, ..., valueN);
Here,
- column1, column2,...columnN are the names of the columns in the table into which you want to insert data.
- The target column names can be listed in any order. The values supplied by the VALUES clause or query are associated with the explicit or implicit column list left-to-right.
You may not need to specify the column(s) name in the SQL query if you are adding values for all the columns of the table. But, make sure the order of the values is in the same order as the columns in the table.
Then SQL INSERT INTO syntax would be as follows −
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, ..., valueN);
Output
Following the table summarizes the output messages and their meaning −
S.No | Output Message & Description |
---|---|
1 |
INSERT oid 1 Message returned if only one row was inserted. oid is the numeric OID of the inserted row. |
2 |
INSERT 0 # Message returned if more than one rows were inserted. # is the number of rows inserted. |
Example of INSERT Statement
Let us create COMPANY table in testdb as follows −
CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL, JOIN_DATE DATE );
Now, use insert query to add a row into the COMPANY table −
INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES (1, 'Paul', 32, 'California', 20000.00, '2001-07-13');
In this query, we omit the salary column and therefore it will have the default value.
INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, JOIN_DATE) VALUES (2, 'Allen', 25, 'Texas', '2007-12-13');
Below the insert query uses DEFAULT clause for the JOIN_DATE column rather than specifying a value −
INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES (3, 'Teddy', 23, 'Norway', 20000.00, DEFAULT );
Here, we inserts multiple rows using the multirow VALUES syntax −
INSERT INTO COMPANY (ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00, '2007-12-13' ), (5, 'David', 27, 'Texas', 85000.00, '2007-12-13');
All the above statements would create the following records in COMPANY table. The next chapter will teach you how to display all these records from a table.
ID | NAME | AGE | ADDRESS | SALARY | JOIN_DATE |
---|---|---|---|---|---|
1 | Paul | 32 | California | 20000.0 | 2001-07-13 |
2 | Allen | 25 | Texas | 2007-12-13 | |
3 | Teddy | 23 | Norway | 20000.0 | |
4 | Mark | 25 | Rich-Mond | 65000.0 | 2007-12-13 |
5 | David | 27 | Texas | 85000.0 | 2007-12-13 |
To generate the above table, use the single insert statement −
INSERT INTO your_table_name (ID, NAME, AGE, ADDRESS, SALARY, JOIN_DATE) VALUES (1, 'Paul', 32, 'California', 20000.0, '2001-07-13'), (2, 'Allen', 25, 'Texas', NULL, '2007-12-13'), (3, 'Teddy', 23, 'Norway', 20000.0, NULL), (4, 'Mark', 25, 'Rich-Mond', 65000.0, '2007-12-13'), (5, 'David', 27, 'Texas', 85000.0, '2007-12-13');
Therefore, this is the way to store the tabular data information in PostgreSQL.