Skip to content
Advertisement

Including a date/time in a file name dumped from psql

I’m planning on running a .sh script that will run periodically through cron on linux. I’m running postgres 8.4 on centos.

My script will have something like this in it:

psql -U username -d db_name -c "COPY orders TO stdout DELIMITER ',' CSV HEADER;" > orders.csv

I know there are other ways to dump tables into csv files but this is the only one I could use without admin rights.

My problem is naming the files. I want to specifically name the file something along the lines of: yyyymmdd-hhmm-orders.csv

I’m not the best scripting guru out there (as you can tell) so how can I get the dumps to dynamically do this?

Thanks

Advertisement

Answer

`date '+%Y%m%d-%H%M'`-orders.csv

I personally also add the seconds %S to the file name

man date

Will show the other formatting options

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement