Skip to main content

Command Palette

Search for a command to run...

Inserting Data into a Table (PostgreSQL)

Published
3 min read
M

As a former 3D Animator with more than 12 years of experience, I have always been fascinated by the intersection of technology and creativity. That's why I recently shifted my career towards MERN stack development and software engineering, where I have been serving since 2021.

With my background in 3D animation, I bring a unique perspective to software development, combining creativity and technical expertise to build innovative and visually engaging applications. I have a passion for learning and staying up-to-date with the latest technologies and best practices, and I enjoy collaborating with cross-functional teams to solve complex problems and create seamless user experiences.

In my current role as a MERN stack developer, I have been responsible for developing and implementing web applications using MongoDB, Express, React, and Node.js. I have also gained experience in Agile development methodologies, version control with Git, and cloud-based deployment using platforms like Heroku and AWS.

I am committed to delivering high-quality work that meets the needs of both clients and end-users, and I am always seeking new challenges and opportunities to grow both personally and professionally.

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:

  • country

  • population

  • area

To do this, we will use SQL again.


Using INSERT INTO

To 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 data

  • cities → the table name

  • (name, country, population, area) → columns we are providing values for

  • values → keyword before actual data

  • Text 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

Best practice:
👉 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.

For example:

insert into cities (name, country, population, area)
values ('Tokyo', 'Japan', 38505000, 8223);

Here:

  • Tokyo → goes into name

  • Japan → goes into country

  • 38505000 → goes into population

  • 8223 → goes into area

If 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

At this point:

  • 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