Created
July 15, 2021 19:34
-
-
Save strboul/f493034f8ebb4d29d8d317ae9fd05f66 to your computer and use it in GitHub Desktop.
Import CSV files into PostgreSQL database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prepare_csv_data_import() { | |
schema_file="$(<yellow_tripdata.schema.template.sql)" | |
csv_files=( data/* ) | |
out="" | |
for csv_file in "${csv_files[@]}"; do | |
tablename="$(basename "${csv_file%.*}")" | |
str_schema="${schema_file//\{TABLENAME\}/${tablename}}" | |
str_copy_query="COPY ${tablename} FROM '/${csv_file}' DELIMITER ',' CSV HEADER;" | |
out="${out}\n\n${str_schema}\n${str_copy_query}" | |
done | |
echo -e "${out}" | |
} | |
docker-compose exec postgres \ | |
psql \ | |
-v ON_ERROR_STOP=1 \ | |
-U docker -d nyc_tlc \ | |
-c "$(prepare_csv_data_import)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment