Transferring Data Between CSV and PostgreSQL
Importing CSV into PostgreSQL
To import a CSV file into PostgreSQL, you can use the COPY
command. The syntax for the COPY
command is as follows:
COPY table_name FROM 'file_path' DELIMITER ',' CSV HEADER;
For example, to import a CSV file named data.csv
into a table named users
, you would use the following command:
COPY users FROM '/path/to/data.csv' DELIMITER ',' CSV HEADER;
Exporting PostgreSQL to CSV
To export a PostgreSQL table to a CSV file, you can use the COPY
command. The syntax for the COPY
command is as follows:
COPY table_name TO 'file_path' DELIMITER ',' CSV HEADER;
For example, to export a table named users
to a CSV file named data.csv
, you would use the following command:
COPY users TO '/path/to/data.csv' DELIMITER ',' CSV HEADER;
Additional Notes
When importing or exporting CSV files, it is important to ensure that the data is formatted correctly. The delimiter character must be specified correctly, and all data values must be enclosed in quotes. Additionally, the header row must be included in the CSV file if the HEADER
option is used.
Comments