PostgreSQL INSERT
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.
Command to insert data
View data
This is from where you can check data using PgAdmin III.
This is the data we have entered using INSERT command -
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);
?>
Previous: Create Tables
Next: Update Data
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/PostgreSQL/PostgreSQL-insert.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics