Automating export of query results to .csv

PaulKraemer's profile image PaulKraemer posted 3 months ago in General Permalink

Hi,

I just installed HeideSQL and created a session in which I connect to a PostreSQL database. I've never used HeideSQL before, but I have experience with SQL on other platforms and utilities. I've created a simple Query for which (when executed), the results are returned on-screen in a pane located in the middle of the HeideSQL window, just below where I typed the Query. If I right-click on the query results and select "Export Grid Rows", I can export the results of my Query to a .CSV file. I foresee myself wanting to do this regularly for multiple queries. With this in mind, I'm wondering if anyone here can tell me if any of the following are possible:

  1. Write a query (SELECT statement) that would automatically direct the results to a .CSV file (without me having to manually chose "Export Grid Rows" and choose a filename every time).
  2. If (1) above is possible, can I have the filename automatically generated? Most likely I'd like to append the date and time on which the query was run to the filename, to create filenames like MyExport_20240422_1725.csv, for example.
  3. If (2) is possible, can I run multiple queries setup like this in a batch?

Any suggestions will be greatly appreciated.

Thanks in advance, Paul

ansgar's profile image ansgar posted 3 months ago Permalink

Automation of HeidiSQL is very limited, and mostly refers to the connection itself, not to things which are done after the connect. There is a section in the help document explaining all supported command line arguments.

For automating exports and other things, you will need to use different tools. For MySQL and MariaDB I always recommend mysqldump. Maybe there is a similar tool for PostgreSQL, I don't know.

PaulKraemer's profile image PaulKraemer posted 3 months ago Permalink

Thank you Ansgar,

Before I worry about automating exports, I'll try to get some practice creating the queries I want an running them manually.

I appreciate your help.

Best regards, Paul

Please login to leave a reply, or register at first.