Exporting a Postgres database to CSV without superuser privileges

How do you export a Postgres database to CSV when you do NOT have superuser privileges?

The line of code in question (for my Bargain Stock Funds project at https://github.com/jhsu802701/bsf-scrape/blob/master/scrape.rb) is:

  @conn.exec("COPY funds TO '" + csv_path + "' With CSV HEADER;")

I need superuser access in order to run this code. This is OK in the development environment, because I used my root privileges (through sudo) to give my username superuser status for Postgres.

However, this does not work in the production environment. where so my username doesn’t have superuser status.

Is there another way to create a CSV file from a Postgres database?

No you don't. You need select privileges on the table, and write permissions in the output directory.

It would far more portable to output a CSV directly using the CSV library (iterating over the results yourself) - also allows for formatting, etc.

On a related note, you may want to look into something like Ruby’s Struct or attr_accessors rather than writing a giant ball of Java-flavored Ruby like your Firm class. :slight_smile:

–Matt Jones