Skip to content

Instantly share code, notes, and snippets.

@moroz
Created July 4, 2023 08:08
Show Gist options
  • Save moroz/8e2a787aea172e3e68156b5798d7fdb8 to your computer and use it in GitHub Desktop.
Save moroz/8e2a787aea172e3e68156b5798d7fdb8 to your computer and use it in GitHub Desktop.
Import a CSV file into postgres with dynamic paths
#!/bin/bash -e
SAMPLES_PATH="$(pwd)/samples.csv"
uname="$(uname)"
# When on Windows, you need to pass the path as c:/path/to/file
# rather than /c/path/to/file (as expected by PostgreSQL).
if [[ "$uname" != "Linux" && "$uname" != "Darwin" ]]; then
# remove the leading slash and replace the second slash with :/
SAMPLES_PATH="$(echo $SAMPLES_PATH | sed 's|/||;s|/|:/|')"
fi
psql <<- EOF
\\copy samples from '$SAMPLES_PATH' csv header;
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment