- Home
- PostgreSQL
- Copy Contents of PostgreSQL Table to CSV File w/ Headers
Copy Contents of PostgreSQL Table to CSV File w/ Headers
Whether migrating to another server or dumping data to an Excel friendly server, PostgreSQL’s cover has got you covered. This turorial will cover how to save to csv format
COPY "myTableName" TO '/tmp/myCsvFile.csv' DELIMITER ',' CSV HEADER;
It’s that easy. Run this in psql client shell in your Linux server.
If you see the following error about having to be a super user, use the following \COPY query from psql:
ERROR: must be superuser to COPY to or from a file HINT: Anyone can COPY to stdout or from stdin. psql's \copy command also works for anyone.
To avoid this error ad a backslash before the COPY:
\COPY "myTableName" TO '/tmp/myCsvFile.csv' DELIMITER ',' CSV HEADER;