Export specific rows from a PostgreSQL table as INSERT SQL script

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for Export specific rows from a PostgreSQL table as INSERT SQL script

📝 Exporting specific rows from a PostgreSQL table as INSERT SQL script

Are you struggling to export specific rows from a PostgreSQL table as an INSERT SQL script? Don't worry, I've got you covered! In this blog post, I'll guide you through some easy solutions to this common problem.

But before we dive into the solutions, let's understand the context of the question. We have a PostgreSQL database schema called "nyummy" and a table named "cimory." The table structure looks like this:

create table nyummy.cimory (
  id numeric(10,0) not null,
  name character varying(60) not null,
  city character varying(50) not null,
  CONSTRAINT cimory_pkey PRIMARY KEY (id)
);

Now, let's address the main question: How can we export data from the "cimory" table as an insert SQL script, but only for records where the city is equal to 'tokyo'?

  1. Using pgAdmin III:

Unfortunately, the question states that pgAdmin III doesn't have an option to export specific rows based on a condition. But don't worry, we have other solutions!

  1. Using pgAdmin 4:

If you have pgAdmin 4 installed, you can easily export specific rows from the "cimory" table. Here are the steps:

  • Open pgAdmin 4 and connect to your PostgreSQL server.

  • Navigate to the "Object Browser" on the left panel and expand the "Servers" and your server.

  • Expand your database schema "nyummy" and the "Tables" section.

  • Right-click on the "cimory" table and select "Backup..."

  • In the "Backup Options" tab, make sure to select the "Plain" format.

  • In the "Dump Options" tab, scroll down to the "Dump Only" section.

  • In the "Where" clause, type city = 'tokyo'.

  • Click the "Backup" button to save the INSERT SQL script file.

  1. Using the command line:

If you prefer the command line interface, you can use the pg_dump utility to export specific rows. Here's an example command:

pg_dump -t nyummy.cimory --where="city = 'tokyo'" > output.sql

This command exports the "cimory" table from the "nyummy" schema, including only the rows where the city is 'tokyo'. The output is redirected to a file named "output.sql".

Feel free to replace "output.sql" with the desired file name.

Now that you have learned a couple of solutions, go ahead and choose the one that suits you best. Export your specific rows from the PostgreSQL table, be it using pgAdmin or the command line.

🔥 Call-to-Action:

Did you find this blog post helpful? Let me know in the comments section below! If you have any other questions or need further assistance, feel free to ask. And don't forget to share this blog post with your fellow PostgreSQL enthusiasts. Happy exporting! 🚀

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my