Inserting Data into a Table (PostgreSQL) | HiringMine
Community · 3 min read Inserting Data into a Table (PostgreSQL) Now that our cities table is ready, the next step is to add data into it. We want to store information about these cities: - Tokyo - Delhi - Shanghai - Sao Paulo Along wit
MS Muhammad Sufiyan Software Engineer · Jul 26
Backend Engineering Hub T-
Now that our cities table is ready, the next step is to add data into it .
We want to store information about these cities:
Tokyo Delhi Shanghai Sao Paulo Along with:
To do this, we will use SQL again.
Using INSERT INTOTo add data into a table, PostgreSQL provides the INSERT INTO command.
The basic structure looks like this:
insert into table_name (column1, column2, ...)
values (value1, value2, ...);
Let’s apply this to our cities table.
Inserting a Single Row Here is the SQL to insert Tokyo into the cities table:
insert into cities (name, country, population, area)
values ('Tokyo', 'Japan', 38505000, 8223);
Important things to notice insert into → tells PostgreSQL we want to add datacities → the table name(name, country, population, area) → columns we are providing values forvalues → keyword before actual dataText values use single quotes Numbers do not use quotes After running this query, PostgreSQL will respond with something like:
Insert successful — 1 row added About the Semicolon (;) You’ll notice a semicolon at the end of the SQL statement.
Technically:
some tools allow running SQL without it but it is part of SQL standard 👉 Always end SQL statements with a semicolon, especially when writing multiple queries.
Column Order Matters The order of columns must match the order of values.
insert into cities (name, country, population, area)
values ('Tokyo', 'Japan', 38505000, 8223);
Tokyo → goes into nameJapan → goes into country38505000 → goes into population8223 → goes into areaIf you change the column order, the values must change too.
Otherwise, PostgreSQL will either:
throw an error or insert wrong data into wrong columns
Inserting Multiple Rows at Once Instead of running INSERT again and again, PostgreSQL allows us to insert multiple rows in one query .
This is done by separating value groups with commas.
Example insert into cities (name, country, population, area)
values
('Delhi', 'India', 28125000, 2240),
('Shanghai', 'China', 22125000, 4015),
('Sao Paulo', 'Brazil', 20935000, 3043);
What’s happening here? Each set of parentheses represents one row All rows are inserted in a single SQL statement Much faster and cleaner than multiple inserts After running this, PostgreSQL will respond with:
Insert successful — 3 rows added
What We Have So Far the cities table exists it has data inside it each city is stored as a separate row Now our table actually contains meaningful information.
What’s Next? The next step is to retrieve data from the table.
In the next blog, we’ll learn how to use SELECT to:
read all cities view specific columns fetch data from the database HIRINGMINE CAREER SIGNAL This writing is proof of expertise. Explore the author’s verified skills, projects and availability—or start a professional conversation.
View career profile Hire this authorMS WRITTEN BY Muhammad Sufiyan Software Engineer. Writing about practical work and career growth.
View profile