Skip to main content
St Louis

Back to all posts

How to Export A Postgresql Table?

Published on
5 min read
How to Export A Postgresql Table? image

Best PostgreSQL Tools to Buy in October 2025

1 Full-Stack Web Development with TypeScript 5: Craft modern full-stack projects with Bun, PostgreSQL, Svelte, TypeScript, and OpenAI

Full-Stack Web Development with TypeScript 5: Craft modern full-stack projects with Bun, PostgreSQL, Svelte, TypeScript, and OpenAI

BUY & SAVE
$36.26
Full-Stack Web Development with TypeScript 5: Craft modern full-stack projects with Bun, PostgreSQL, Svelte, TypeScript, and OpenAI
2 PostgreSQL: A Practical Guide for Developers and Data Professionals

PostgreSQL: A Practical Guide for Developers and Data Professionals

BUY & SAVE
$5.99
PostgreSQL: A Practical Guide for Developers and Data Professionals
3 Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-Centric Applications with PL/pgSQL

Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-Centric Applications with PL/pgSQL

BUY & SAVE
$39.91
Procedural Programming with PostgreSQL PL/pgSQL: Design Complex Database-Centric Applications with PL/pgSQL
4 Beginning PHP and PostgreSQL 8: From Novice to Professional (Beginning: From Novice to Professional)

Beginning PHP and PostgreSQL 8: From Novice to Professional (Beginning: From Novice to Professional)

  • HIGH-QUALITY: ENJOY GREAT READS AT A FRACTION OF THE PRICE.
  • ECO-FRIENDLY: CHOOSE SUSTAINABILITY WITH PRE-LOVED BOOKS.
  • UNIQUE FINDS: DISCOVER RARE TITLES NOT AVAILABLE IN STORES.
BUY & SAVE
$35.25 $49.99
Save 29%
Beginning PHP and PostgreSQL 8: From Novice to Professional (Beginning: From Novice to Professional)
5 DEUOTION T-post Clips Tool, Fixing Fence Clip and Wire Steel Bender T-post Handheld Twisting Tool, Multi Functional Bender

DEUOTION T-post Clips Tool, Fixing Fence Clip and Wire Steel Bender T-post Handheld Twisting Tool, Multi Functional Bender

  • SPEED UP FENCE INSTALLATION WITH EFFICIENT T-POST CLIPS TOOL!

  • USER-FRIENDLY DESIGN FOR QUICK AND EASY OPERATION FOR EVERYONE!

  • DURABLE STEEL CONSTRUCTION ENSURES LONG-LASTING PERFORMANCE OUTDOORS!

BUY & SAVE
$16.99
DEUOTION T-post Clips Tool, Fixing Fence Clip and Wire Steel Bender T-post Handheld Twisting Tool, Multi Functional Bender
6 Mastering PostgreSQL for Developers: Building Fast, Secure, and Scalable Apps

Mastering PostgreSQL for Developers: Building Fast, Secure, and Scalable Apps

BUY & SAVE
$6.77
Mastering PostgreSQL for Developers: Building Fast, Secure, and Scalable Apps
7 PostgreSQL for Python Web Development with Flask: A Practical Guide to Building Database-Driven Web Applications

PostgreSQL for Python Web Development with Flask: A Practical Guide to Building Database-Driven Web Applications

BUY & SAVE
$7.99
PostgreSQL for Python Web Development with Flask: A Practical Guide to Building Database-Driven Web Applications
8 Century Drill & Tool 72898 Post Level

Century Drill & Tool 72898 Post Level

  • HANDS-FREE LEVELING WITH MAGNETS & STRAPS FOR EASY, PRECISE WORK.
  • VERSATILE FOR POSTS, POLES, AND VARIOUS STRUCTURES-INSTALL WITH EASE!
  • DUAL LOCKING ARMS ENSURE STABILITY & ACCURACY AT A PERFECT 90° ANGLE.
BUY & SAVE
$11.98
Century Drill & Tool 72898 Post Level
+
ONE MORE?

To export a Postgresql table, you can follow these steps:

  1. Open a command-line interface or terminal and navigate to the directory where you want to save the exported file.
  2. Use the pg_dump command followed by the name of the database and table you want to export. The basic syntax is: pg_dump -t -f Replace with the name of the table you want to export, with the desired name for the exported file, and with the name of the database that contains the table. For example, to export a table named "employees" from a database named "company" and save it as "employees_backup.sql", the command would be: pg_dump -t employees -f employees_backup.sql company
  3. Run the command, and Postgresql will generate a SQL script containing the table structure and data. The output file will be saved in the specified directory. You can also add additional options to the pg_dump command to specify different formats or settings for the exported data. Refer to the Postgresql documentation for more advanced options.
  4. You have successfully exported the Postgresql table as a SQL script. The exported file can be used to restore the table structure and data later on if needed.

How to export a specific column from a Postgresql table?

To export a specific column from a PostgreSQL table, you can use the COPY command with a query. Here's how you can do it:

  1. Open the terminal and connect to your PostgreSQL database.
  2. Use the COPY command with the desired query to export the specific column.

COPY (SELECT column_name FROM table_name) TO '/path/to/exported_file.csv' DELIMITER ',' CSV HEADER;

Replace column_name with the name of the column you want to export and table_name with the name of the table containing the column. Also, replace /path/to/exported_file.csv with the path and filename where you want to save the exported data.

  1. Run the command to export the column. The exported file will be created at the specified location.

You can change the delimiter and file format based on your requirements. For example, instead of CSV, you can use TXT, JSON, etc.

How to export a Postgresql table as a pipe-separated file (PSV)?

To export a PostgreSQL table as a pipe-separated file (PSV), you can use the PostgreSQL command-line utility called pg_dump in combination with the COPY command. Here's how you can do it:

  1. Open the command-line interface (CLI) or terminal.
  2. Navigate to the directory where you want to save the PSV file.
  3. Run the following command to export the table and save it as a PSV file:

pg_dump -t <table_name> -O -c -v -F p <database_name> | psql -h <host_name> -p <port_number> -U -d <destination_database> -c "COPY <table_name> TO STDOUT (FORMAT CSV, DELIMITER '|')"

Make sure to replace <table_name> with the name of the table you want to export, <database_name> with the name of the database containing the table, <host_name> with the PostgreSQL server's hostname, <port_number> with the port number used by the PostgreSQL server, <username> with your PostgreSQL username, and <destination_database> with the name of the destination database (can be the same as the source).

For example, if you have a table called "customers" in a database named "ecommerce", and your PostgreSQL server is running on localhost with the default port 5432, you can run the following command to export the table:

pg_dump -t customers -O -c -v -F p ecommerce | psql -h localhost -p 5432 -U your_username -d ecommerce -c "COPY customers TO STDOUT (FORMAT CSV, DELIMITER '|')" > customers.psv

This command will export the "customers" table from the "ecommerce" database, using a pipe-separated (PSV) format, and save it in a file named "customers.psv".

Note: Make sure you have the necessary permissions to run the pg_dump and COPY commands on the table and database.

How to export a Postgresql table to a compressed file (ZIP/RAR)?

To export a PostgreSQL table to a compressed file like ZIP or RAR, you can follow these steps:

  1. Install the necessary tools: Install PostgreSQL if you haven't already. Install the ZIP or RAR utility if you haven't already. In the case of ZIP, it might already be available in your system.
  2. Open a command prompt or terminal.
  3. Use the pg_dump command to export the table data from PostgreSQL as a SQL text file. Run the following command: pg_dump -t -f .sql Replace with the name of the table you want to export, with the desired name for the output file, and with the name of your PostgreSQL database.
  4. Compress the exported SQL file using the appropriate utility: To compress using ZIP, run the following command: zip .zip .sql To compress using RAR, run the following command: rar a .rar .sql Replace with the same name you used while exporting the SQL file.
  5. The compressed file .zip or .rar should now be created in the current directory.

You can now transfer or share the compressed file as needed. Note that the SQL file inside the compressed archive can be extracted and imported into PostgreSQL using the pg_restore command, if required.