1

I'm using psql to run a .sql file to copy data from csv files into Postgres, but the folder contains csv files is changed everyday, so I want to add a variable into path code

This is my code

delete from table1;
delete from table2;
delete from table3;
\copy table1 FROM 'D:\tamlam\' + convert(date,getdate()) + '\file1.csv' DELIMITER ',' CSV HEADER QUOTE '"' NULL '';
Tam Lam
  • 31
  • 3

1 Answers1

0

Use the PROGRAM option.

See this documentation how to get date in a desired format in Windows batch.

Put that in a script which does TYPE %filenamevariable% at the end. Let's say the script is named data_gen. You may then do

\COPY table1 FROM PROGRAM '\scriptpath\data_gen'

For Unix users, it is simpler, date command with options can generate date in a format and set a variable. then simply do cat $filenamevariable at the end of program

Kaushik Nayak
  • 30,772
  • 5
  • 32
  • 45