w3resource logo


PostgreSQL INSERT

PostgreSQL INSERT

Secondary Nav

INSERT Command

This document discusses how to insert data into a table using PostgreSQL INSERT command. We have also covered how to do the same using PHP-PostgreSQL.KJVQCSV69JFX

Usage

Following is the usage of PostgreSQL INSERT command for inserting data into a single row of a PostgreSQL table.

INSERT INTO table_name (column1, column2, column3 .....) VALUES (value1, value2, value3....)

Where table_name is the associated table, column1, 2, 3 are column names and value 1, 2, 3 are values to be inserted.

Insert single row

Structure of the table

Following is the structure of the table where we will insert data.

insert data table structure

Command to insert data

insert postgresql

View data

This is from where you can check data using PgAdmin III.

Command to insert data

 

This is the data we have entered using INSERT command -

view data postgresql

 

Insert multiple rows

Here is the command to insert multiple rows in a PostgreSQL database.

INSERT INTO book (book_id, name, price, date_of_publication) VALUES ('HTML01', 'HTML Unleashed', 19.00, '08-07-2010'), ('JS01', 'JavaScript Unleashed', 22.00, '01-05-2010'), ('PHP01', 'PHP Unleashed', 19.00, '01-04-2010');

Insert data : PHP-PostgreSQL

The following example code is to insert data into the 'book' table using PHP. Assume that the name of the file saved with this code is insert.php.

<!DOCTYPE html>
<head>
<title>Insert data to PostgreSQL with php - creating a simple web application</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style>
li {
list-style: none;
}
</style>
</head>
<body>
<h2>Enter data into book table</h2>
<ul>
<form name="insert" action="insert.php" method="POST" >
<li>Book ID:</li><li><input type="text" name="bookid" /></li>
<li>Book Name:</li><li><input type="text" name="book_name" /></li>
<li>Price (USD):</li><li><input type="text" name="price" /></li>
<li>Date of publication:</li><li><input type="text" name="dop" /></li>
<li><input type="submit" /></li>
</form>
</ul>
</body>
</html>
<?php
$db = pg_connect("host=localhost port=5432 dbname=postgres user=postgres password=myadmin123");
$query = "INSERT INTO book VALUES ('$_POST[bookid]','$_POST[book_name]',
'$_POST[price]','$_POST[dop]')";
$result = pg_query($query); 
?>


Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number