0

I'm trying to read CSV file and writing the same into the table, CSV file was located in my local machine(client). I used /copy command and achieved the same. Here I have hardcoded my filepath in sql script. I want to parameterised my csv file path.

Based on my analysis /copy not supported :variable substitution, but not sure

I believe we can achieve this using shell variables, but I tried the same, It's not working as expected.

Following are my sample scripts

command:

psql  -U postgres -h localhost testdb  -a -f '/tmp/psql.sql' -v path='"/tmp/userData.csv"'

psql script:

  \copy test_user_table('username','dob') from :path DELIMITER ',' CSV HEADER;
  

I executing this commands from shell and I'm getting no such a file not found exception. But same script is working with hardcoded path.

Anyone able to advise me on this.

Reference :

Variable substitution in psql \copy

https://www.postgresql.org/docs/devel/app-psql.html

Debugger
  • 690
  • 1
  • 18
  • 41

1 Answers1

0

I am new to Bash. So far your problem is way hard for me.
I can do it in one shell script. Maybe later I can make it to two scripts. The follow is a simple one script file.

#!bin/bash
p=\'"/mnt/c/Users/JIAN HE/Desktop/test.csv"\'
c="copy emp from ${p}"

a=${c}
echo $a
psql -U postgres -d postgres  -c "${a}"
jian
  • 4,119
  • 1
  • 17
  • 32