brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: \pset null makes NULL visible in psql so it stops looking like an empty PostgreSQL string

Postgresql
~/tips/tip-postgresql-psql-pset-null

\pset null '(null)'

Prints a marker for NULL values so you can tell them apart from empty text.

psql prints NULL as nothing at all. An empty string also prints as nothing. When a query behaves oddly, telling those two apart is often the whole puzzle.

\pset null '(null)'
SELECT id, email, phone FROM customers LIMIT 3;
 id |      email      | phone
----+-----------------+--------
  1 | [email protected] |
  2 | (null)          | (null)

Row 1 has an empty phone string, row 2 has no value at all. That difference decides whether WHERE phone = '' or WHERE phone IS NULL finds it, and why a count(phone) gives a lower number than you expected. The docs themselves warn that the default empty output is easy to mistake for an empty string.

I like a marker that real data never contains. Some people use the Unicode symbol ∅, which is short and stands out, if your terminal font has it. Add the line to ~/.psqlrc so every session has it.

The setting only changes how psql displays results. Exports with \copy are not affected by it, which is what you want. But a script that parses psql’s normal aligned output will now see the marker as text. Keep that in mind if anything reads your psql output, and run such scripts with psql -X so your personal settings stay out.

Further reading: psql (\pset null)

More Postgresql tips FROM pg_settings FROM pg_hba_file_rules LOAD 'auto_explain'; CREATE EXTENSION pgstattuple; pg_dump -O -x -f mydb.sql mydb All Postgresql tips (24) All topics

Link