SQL files and result export

SQL files in query tabs, the file dialog, and the export of a whole result set as CSV, JSON or XML.

All paths are client-side: the host pgtc runs on — over SSH that is the remote host — never the database server.

Opening a SQL file

File → Open SQL file… (also in the tree's context menu, F2) loads the file into a new query tab on the database selected in the tree. Nothing is executed. Without a selected database the command is disabled.

Open SQL file dialog
Open SQL file.

Saving

The file dialog

One dialog for all file commands (SQL files, result export, .pgdef), modelled on Midnight Commander's. OK takes whichever was used last: the selected row or the typed text.

TypedResolved as
report.sql, reports/q3.sql, ../x.sqlRelative to the listed directory.
D:\work\x.sql, /home/me/x.sql, \\server\share\x.sqlAbsolute.
~, ~/sql/x.sqlThe home directory — also on Windows.
D:Windows: the root of drive D.

Places: drives and other starting points

Places in the file dialog
Places on Windows.

Alt+P (Places) lists: the home directory; the folder of the tab's file; on Windows every drive with its label; on Linux / and the entries under /media/<user>, /run/media/<user> and /mnt; on macOS / and /Volumes. On Windows, .. in the root of a drive also leads here.

Exporting a result

Query → Export result… (or F2 in the grid) writes the selected result set as CSV, JSON or XML. pgtc writes the rows itself: no COPY, no extra privileges.

Export result dialog
The file dialog plus the format options.

CSV

territoryid,name,countryregioncode,salesytd
1,Northwest,US,7887186.7882
6,Canada,CA,6771829.1376
PropertyWritten as
HeaderOne line with the column names.
Separator, (default), ; or tab.
QuotingRFC 4180: only fields containing the separator, a double quote or a line break; " is doubled.
NULLAn empty field — indistinguishable from an empty string (unlike COPY … CSV).
Line endsCR LF.
EncodingUTF-8; a BOM if CSV with UTF-8 BOM is checked.

For Excel: check the BOM, and choose ; where the locale's decimal separator is a comma.

JSON

[
  {"territoryid": 1, "name": "Northwest", "countryregioncode": "US", "salesytd": 7887186.7882},
  {"territoryid": 6, "name": "Canada", "countryregioncode": "CA", "salesytd": 6771829.1376}
]

One object per row; the JSON type follows the column's type OID:

Column typeIn JSON
int2, int4, int8, oid, float4, float8, numericA number: the digits of the text output, copied unchanged (full precision in the file). NaN, Infinity, -Infinity are written as strings.
booltrue / false
json, jsonbEmbedded as a JSON value.
SQL NULLnull
Every other typeA string with the text output.

Duplicate column names are made unique: the second a becomes a_2.

XML

<?xml version="1.0" encoding="UTF-8"?>
<result>
  <columns><column name="territoryid"/><column name="name"/></columns>
  <row><value>1</value><value>Northwest</value></row>
  <row><value>6</value><value null="true"/></row>
</result>

Other ways to get data out