Site icon Database Tutorials

PostgreSQL copy command Example

This article contains information about PostgreSQL copy command example such as exporting query result to csv, importing file to postgres.

What is PostgreSQL copy command?

PostgreSQL copy command Example

Export table in PostgreSQL

We can copy the table to the client by using the vertical bar (|) as a delimiter between columns with the following command.

We can copy the table to the file we want by using spaces as delimiter between columns with the following command.

Import file to PostgreSQL

We can transfer the data in the file to our existing table with the following command.

Export query result to file in PostgreSQL

We can export the result of a query to a file.

If we want to compress the output we will get with the program, we can use the following command.

If we want to export only 2 columns, we can use the following command.

If we want to export in binary format, we can get it as follows.

If we want to export in CSV format, we can get it as follows. If you want you can export query result to csv in postgres in this way.

There are a few basic terms we need to know about CSV files;

DELIMITER – Delimiter is a character that separates each row of the file into columns; In the CSV file, the delimiter is comma.

HEADER – When a CSV file is created, the header row is the first line of the file containing the column names. If you don’t want column names to be copied to the CSV file, you can ignore HEADER.

For example, if you have Turkish characters, you can use encoding as follows.

 

Exit mobile version