Inserting Data into PostgreSQL Tables
Learn how to use the INSERT INTO statement in PostgreSQL to efficiently add single or multiple rows of data to your database tables. This guide provides clear examples and explanations for inserting data into PostgreSQL.
PostgreSQL Insert Data
Insert Into
To add data into a table in PostgreSQL, the INSERT INTO statement is used.
The following example demonstrates how to insert a single row of data into the cars
table:
Syntax
INSERT INTO cars (brand, model, year)
VALUES ('Honda', 'Civic', 1999);
Output
After running the above SQL command, the SQL Shell application will display:
Output
INSERT 0 1
This indicates that one row was inserted successfully. The number 0 represents an internal value that can be ignored for now.
SQL Statement Explained
Here are some important points about the INSERT INTO
statement:
- String values must be enclosed in single quotes (apostrophes).
- Numeric values can be written without quotes, although including them is optional.
Display Table
To verify the inserted data, use the following SQL statement to display the table:
Syntax
SELECT * FROM cars;
Output
Executing this command will show the following table:
Output
brand | model | year
-------+---------+------
Honda | Civic | 1999
(1 row)
Insert Multiple Rows
To insert multiple rows into a table, use the INSERT INTO
statement with multiple VALUES
entries:
Syntax
INSERT INTO cars (brand, model, year)
VALUES
('Chevrolet', 'Impala', 1967),
('Audi', 'Quattro', 1983),
('Nissan', 'Skyline', 1995);
Output
After executing the command, the SQL Shell will return:
Output
INSERT 0 3
This output confirms that three rows were inserted successfully.
Display Table
To verify all the inserted rows, use the same command to display the table:
Syntax
SELECT * FROM cars;
Output
The table will display the following data:
Output
brand | model | year
------------+-----------+------
Honda | Civic | 1999
Chevrolet | Impala | 1967
Audi | Quattro | 1983
Nissan | Skyline | 1995
(4 rows)