0

I'm interacting with a PostgreSQL on command-line based environment, and I'd like to be able to determine the data types of the table columns in this database.

For a simple example, when I request

SELECT * FROM products

I'd like to know if the product id column it returns is giving me text or integers.

1 Answer 1

2

You can get this kind of information by querying the catalog.

To find the specific queries, run psql -E (to echo hidden query) and then e.g. \d products. You'll see psql output various queries that yield information about your table, its column types, indexes, etc.

In this specific case, you'd run something like:

SELECT a.attname,
  pg_catalog.format_type(a.atttypid, a.atttypmod)
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = 'products'::regclass AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! \d products worked for me, but I didn't understand the code you posted...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.