Created
July 4, 2023 08:08
-
-
Save moroz/8e2a787aea172e3e68156b5798d7fdb8 to your computer and use it in GitHub Desktop.
Import a CSV file into postgres with dynamic paths
This file contains hidden or 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
#!/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