1

I am attempting to export my PSQL table to a CSV file. I have read many tips online, such as Save PL/pgSQL output from PostgreSQL to a CSV file

From these posts I have been able to figure out

\copy (Select * From foo) To '/tmp/test.csv' With CSV

However I do not want to specify the path, I would like the user to be able to enter it via the export shell script. Is it possible to pass args to a .sql script from a .sh script?

Community
  • 1
  • 1
A. Shears
  • 97
  • 1
  • 1
  • 11

1 Answers1

0

I was able to figure out the solution.

In my bash script I used something similar to the following

psql $1 -c "\copy (SELECT * FROM users) TO '"$2"'" DELIMINTER ',' CSV HEADER"

This code copies the user table from the database specified by the first argument and exports it to a csv file located at the second arguement

A. Shears
  • 97
  • 1
  • 1
  • 11