I think I first touched PostgreSQL in 1998 or 1999. It has been a long time, but PostgreSQL keeps inspiring me. I still keep finding cool features which have most likely been around for 15 years or more. Recently, I noticed by coincidence that psql can produce perfect LaTeX code. How cool is that? It’s so cool that I decided to share my happiness with people reading my blog.

Here’s some 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.

Find out more about cool psql tricks – read Julian Markwort’s blog about using psql to automate repeated tasks: gexec in psql for poweruser practice.


In order to receive regular updates on important changes in PostgreSQL, subscribe to our newsletter, or follow us on Twitter, Facebook, or LinkedIn.