To convert a CSV file to a PostgreSQL INSERT
statement, you can use the csvkit
command-line utility. csvkit
is a suite of utilities for working with CSV data, and it includes a csvquote
tool that can be used to convert CSV data to INSERT
statements.
Here is an example of how you might use csvquote
to convert a CSV file to a PostgreSQL INSERT
statement:
csvquote --quote=DOUBLE my_file.csv | psql -c "COPY my_table (column1, column2, column3) FROM STDIN WITH (FORMAT CSV, DELIMITER ',');"
This command uses csvquote
to convert the data in my_file.csv
to a format that is compatible with the COPY
statement, and then it uses psql
to execute the COPY
statement and insert the data into the my_table
table in the PostgreSQL database.
You can customize this command to fit your specific needs and requirements, such as using a different delimiter or specifying a different table name. Once the data is inserted into the table, you can use SQL queries to work with the data as needed.