Logo AppDev24 Login / Sign Up
Sign Up
Have Login?
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Login
New Account?
Recovery
Go to Login
By continuing you indicate that you agree to Terms of Service and Privacy Policy of the site.
Databases

Import & Export Data from PostgreSQL

Updated on Jun 22, 2024

As a developer, managing data is an essential part of our workflow. Whether we're working with large datasets or small, having the ability to import and export data efficiently can save our time and reduce errors. In this article, we'll explore how to use the psql COPY utility command to quickly and easily import and export data from PostgreSQL.

Import Data to Table

To import data into a PostgreSQL table, we can use the following command:

psql -h 127.0.0.1 -p 5432 -d demo -U demouser -W -c "\COPY sales.dept FROM '/Users/sauravmitra/Demo/dept.csv' DELIMITER ',' CSV HEADER"

This command imports data from a comma-delimited file with a header into the sales.dept table. We can also specify specific columns to import by adding them in parentheses, like this:

psql -h 127.0.0.1 -p 5432 -d demo -U demouser -W -c "\COPY sales.dept (deptno, dname) FROM '/Users/sauravmitra/Demo/dept.csv' DELIMITER ',' CSV HEADER"

Export Data from Table

To export data from a PostgreSQL table, we can use the following command:

psql -h 127.0.0.1 -p 5432 -d demo -U demouser -W -c "\COPY sales.dept TO '/Users/sauravmitra/Demo/dept.csv' DELIMITER ',' CSV HEADER"

This command exports data from the sales.dept table into a comma-delimited file with a header. Again, we can specify specific columns to export by adding them in parentheses.

psql -h 127.0.0.1 -p 5432 -d demo -U demouser -W -c "\COPY sales.dept (deptno, dname) TO '/Users/sauravmitra/Demo/dept.csv' DELIMITER ',' CSV HEADER"

Export Data Based on SQL Query

But what if we want to export data based on a specific SQL query? That's where things get really powerful. We can use the following command:

psql -h 127.0.0.1 -p 5432 -d demo -U demouser -W -c "\COPY (SELECT * FROM sales.dept WHERE deptno < 5) TO '/Users/sauravmitra/Demo/dept_filter.csv' DELIMITER ',' CSV HEADER"

This command exports data from the sales.dept table, but only for rows where deptno is less than 5. The resulting file will contain only those rows.

Export Data Based on Complex SQL Query

And finally, what if we have a complex SQL query that we want to use to export data? We can create a SQL query file and then use the following command:

emp_dept.sql

WITH employee AS (
    SELECT 
        empno, ename, mgrno, deptno, sal
    FROM sales.emp
),
department AS (
    SELECT
        deptno AS dnum, dname
    FROM sales.dept 
),
manager AS (
    SELECT
        empno AS mgrnum, ename AS mgrname
    FROM sales.emp
),
result AS (
    SELECT
       empno, ename, deptno, dname, sal, mgrno, mgrname
    FROM employee
    INNER JOIN department
    ON employee.deptno = department.dnum
    LEFT OUTER JOIN manager
    ON employee.mgrno = manager.mgrnum
) 
SELECT * FROM result
psql -h 127.0.0.1 -p 5432 -d demo -U demouser -W -c "\COPY ($(</Users/sauravmitra/Demo/emp_dept.sql)) TO '/Users/sauravmitra/Demo/emp_dept.csv' DELIMITER ',' CSV HEADER"

This command exports data based on the complex SQL query in the emp_dept.sql file, and saves it to a comma-delimited file with a header.

In this article, we've seen how to use the psql COPY utility command to import and export data from PostgreSQL. Whether we're working with small datasets or large ones, these commands can save us time and reduce errors. So next time you need to manage your data, give them a try!

PrimeChess

PrimeChess.org

PrimeChess.org makes elite chess training accessible and affordable for everyone. For the past 6 years, we have offered free chess camps for kids in Singapore and India, and during that time, we also observed many average-rated coaches charging far too much for their services.

To change that, we assembled a team of top-rated coaches including International Masters (IM) or coaches with multiple IM or GM norms, to provide online classes starting from $50 per month (8 classes each month + 4 tournaments)

This affordability is only possible if we get more students. This is why it will be very helpful if you could please pass-on this message to others.

Exclucively For Indian Residents: 
Basic - ₹1500
Intermediate- ₹2000
Advanced - ₹2500

Top 10 Articles