
The COPY command is executed from the viewpoint of the server, not the client. The most common reason permission will be denied is because the Postgres server cannot access the file at the location you specified.
#Postgres copy table how to#
When you're using these commands if you're getting a "Permission Denied" error, here's the likely reason why and how to fix it. Likewise, COPY TO is used to export data from a table or a query result to a file. This post explained the working of the COPY statement and \COPY command with examples.COPY FROM is commonly used to import data from a file (CSV, plain text, etc.) in to PostgreSQL. Using these commands, you can export the entire Postgres table or some specific columns of a table into a CSV file. In PostgreSQL, there are multiple ways to export a table into a CSV file, such as the COPY statement or \COPY command. This is how you can export a Postgres Table to a CSV file using the COPY statement or \COPY command. On successful execution of the \COPY command, following data will be exported to the CSV file: Follow the below-given syntax for the client-side export of CSV files: \COPY Tab_Name to 'Path/file_name.csv' CSV HEADER įor exporting the desired table to a CSV file, execute the \COPY command: \COPY (SELECT * FROM book_info) to 'C:\Windows\Temp\bookInfo.csv' with CSV If you have limited privileges, then you can use the \COPY command because it doesn’t require superuser privileges. The \COPY is an inbuilt command that is used for exporting a PostgreSQL table to a CSV file. How Does the \COPY Command Work in PostgreSQL? In this way, you can export only specific columns of a table. Let’s open the CSV file to see the exported values: And this time, values are separated with “ ”.Įxecute the below statement to export only specific columns of a Postgres table: COPY book_info(book_name) TO 'C:\Windows\Temp\book_info.csv' CSV HEADER The output shows that the desired table has been exported to the CSV file.

Open the CSV file from the targeted location to see the exported values: For instance, the below statement will provide the “ ” separated values: COPY book_info TO 'C:\Windows\Temp\book_info1.csv' DELIMITER ' ' CSV HEADER You can specify a delimiter of your choice to separate the table’s values. The output shows that the desired table has been exported to the CSV file successfully.

If everything goes fine, you will get the following results: The output shows that five records have been exported. Now run the following command to export the book_info table into a CSV file: COPY book_info TO 'C:\Windows\Temp\book_info.csv' CSV HEADER

Let's list down the table details using the SELECT command: SELECT * FROM book_info File_name.csv is the exported CSV file. Path represents a directory where the table will be exported. To achieve this purpose, use the following syntax: COPY tab_name TO 'Path/file_name.csv' CSV HEADER How Does the COPY Statement Work in PostgreSQL?Ĭopy statement is the simplest way to export a table into a CSV file. In this write-up, we will learn different methods to export a Postgres table into a CSV file. In PostgreSQL, you have multiple ways to export a table into a CSV file, such as copy statement, \copy command, or pgAdmin.

While working with databases like PostgreSQL, exporting a table into a CSV file or importing a CSV file into a Postgres table is a very common task.
