Skip to content

Instantly share code, notes, and snippets.

@strboul
Created July 15, 2021 19:34
Show Gist options
  • Save strboul/f493034f8ebb4d29d8d317ae9fd05f66 to your computer and use it in GitHub Desktop.
Save strboul/f493034f8ebb4d29d8d317ae9fd05f66 to your computer and use it in GitHub Desktop.
Import CSV files into PostgreSQL database
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