postgres: how to query column labels for a table



~1 min read


120 words

What if, instead of a table’s data, you wanted to see the table’s metadata? That is, instead of querying all of the rows within a table where x or y are true, you wanted to see a list of all of the columns where z is true.

How would you do that?

It’s actually quite simple:

FROM information_schema.columns
WHERE table_name = 'target_table'
AND table_schema = 'target_schema';

The table_schema is required only if you have multiple schemas you’re choosing between. If all tables are in the public schema or if there’s only a single table with the name, it can be excluded .

If you’re using the command line, it appears that \d+ <target_table> would also work.

Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!