psql output to CSV converter

Paste a query result as psql prints it and get it as CSV, ready for a spreadsheet: the columns are cut where psql's ----+---- rule marks them, and the (3 rows) line is left out. The mysql client's boxed tables and Markdown tables work too.

Straight from the terminal

Copy the result from psql as it is: the line of column names, the rule of dashes under it, the rows, and the row count at the end if you like. The spaces psql adds to line the columns up are trimmed, and a value holding a | stays whole, because cells are cut where the rule has its + marks rather than at every |.

Values on several lines

psql ends a cell with + when its value carries on in the next line, as text with line breaks or pretty-printed JSON does. Those lines are joined back into one value with its line breaks, and the CSV quotes it, so a spreadsheet keeps it in one cell.

MySQL and Markdown tables

The mysql client draws its results in a box, with +----+ borders and | between the columns, and ends with "3 rows in set (0.01 sec)"; Markdown tables from a README, an issue or a wiki have a |---| line under the header. Both are read the same way.

CSV, JSON or YAML

CSV shows below the editor first. The Convert menu above the editor turns the same rows into JSON, an array with an object per row named by the header row (numbers and true or false become JSON numbers and booleans), or into YAML. Format lines up the columns of the pasted table again, in its own style.

If you can run the query again

psql writes CSV itself: psql --csv (from version 12), or \copy (SELECT …) TO 'result.csv' CSV HEADER. This page is for a result you already have, in the terminal's scrollback, a ticket, a chat or a log.

Copy, download or keep editing

The CSV follows your typing, and a mistake in what you paste is pointed out first, with its line and column. Copy or Download take the CSV as it is; Use as text puts it in the editor in place of what you pasted (Ctrl+Z goes back).

Private: nothing is uploaded

Formatting, checking and comparing all happen in your browser. Your documents are saved in the browser's own storage on this computer, not on a server, so they never leave your device.

With your permission, we count anonymous visits (which pages and tools are used), never what you type. See Privacy.

Questions

How do I turn psql output into CSV?

Copy the whole result from the terminal and paste it here: the CSV appears below the editor straight away, to copy, or to download as a .csv file.

What happens to NULL values?

psql prints NULL as an empty cell, so it becomes an empty field. To tell NULL from an empty string, run \pset null NULL before the query: the word NULL then comes through.

Does it read expanded output?

Not yet: expanded display (\x), with a block per record, needs to be switched off with \x before running the query.

Is my data uploaded anywhere?

Your documents aren't: everything runs in your browser, and they are saved on your computer, in the browser's storage for this site. With your permission we count anonymous visits, never their content.

More tools