In order to list or show all of the tables in a Redshift database, you'll need to query the PG_TABLE_DEF
systems table. An interesting thing to note is the PG_
prefix. This is because Redshift is based off Postgres, so that little prefix is a throwback to Redshift’s Postgres origins.
The Unified Stack for Modern Data Teams
Get a personalized platform demo & 30-minute Q&A session with a Solution Engineer
Running SELECT * FROM PG_TABLE_DEF
will return every column from every table in every schema. This means that systems tables will be included and each table will be listed multiple times, one for each column. For this reason, if you just want to get a list of tables, you’ll want to use the following query:
SELECT DISTINCT tablename FROM pg_table_def WHERE schemaname = 'public';
This will return a single column of table names for the public schema, which is probably where most of your data resides.
When we SELECT * FROM PG_TABLE_DEF;
, we get the following columns:
- schemaname
- You most likely want to limit this to “public” otherwise it will also include all of the systems tables.
- tablename
- The name of the table, yay!
- columnname
- The name of each column
- type
- The datatype of each column
- encoding
- distkey and sortkey
- Learn more about Redshift distkey and sortkey.
- notnull
- Boolean to determine if the column has a NOT NULL constraint.
And there you go! Should be pretty simple. Let us know in the comments if you have any questions!