To import a CSV file into a PostgreSQL database using a shell script, you can use the psql
command-line utility. psql
is the command-line interface to the PostgreSQL database, allowing you to execute SQL statements and scripts from the command line.
Here is an example of a shell script that uses psql
to import a CSV file into a PostgreSQL database:
#!/bin/bash
# Set the path to the CSV file
csv_file='/path/to/my_file.csv'
# Set the name of the table that the data will be imported into
table_name='my_table'
# Set the column names for the table
columns='(column1, column2, column3)'
# Import the data from the CSV file into the PostgreSQL table
psql -c "COPY ${table_name} ${columns} FROM '${csv_file}' WITH (FORMAT CSV, DELIMITER ',');"
This script uses the COPY
statement to import the data from the CSV file into the specified PostgreSQL table. You can customize the script to fit your specific needs and requirements, such as specifying different delimiters or using a different format for the file.
Once the script is executed, the data from the CSV file will be inserted into the specified table in the PostgreSQL database.