I think I have touched my first PostgreSQL in 1998 or 1999. It has been a long time but PostgreSQL keeps inspiring me. And still: I keep finding cool features, which have most likely been around for 15 years or more.

By coincidence I noticed that psql can produce perfect LaTeX code. How cool is that? It is so cool that I decided to share my happiness with people reading my blog.

Here is some trivial test data:

test=# CREATE TABLE cool_feature (a int, b int);
CREATE TABLE
test=# INSERT INTO cool_feature VALUES (1, 2), (3, 4);
INSERT 0 2
test=# TABLE cool_feature;
a | b
---+---
1 | 2
3 | 4
(2 rows)

Making psql produce LaTeX is really simple:

test=# \pset format latex
Output format is latex.

From now on the output will be returned in LaTeX format:

test=# TABLE cool_feature;
\begin{tabular}{r | r}
\textit{a} & \textit{b} \\
\hline
1 & 2 \\
3 & 4 \\
\end{tabular}

\noindent (2 rows) \\

This is even true for backslash commands:

test=# \d
\begin{center}
List of relations
\end{center}

\begin{tabular}{l | l | l | l}
\textit{Schema} & \textit{Name} & \textit{Type} & \textit{Owner} \\
\hline
public & cool\_feature & table & hs \\
public & pg\_stat\_statements & view & hs \\
public & pgbench\_accounts & table & hs \\
public & pgbench\_branches & table & hs \\
public & pgbench\_history & table & hs \\
public & pgbench\_tellers & table & hs \\
\end{tabular}

\noindent (6 rows) \\

PostgreSQL can also produce a variety of other formats, which might be useful to some people: aligned, asciidoc, html, latex, latex-longtable, troff-ms, unaligned, and wrapped.

04