List All Tables and Columns in Redshift or Postgres

The short answer

Query SVV_TABLES for tables and SVV_COLUMNS for columns. They are the Redshift equivalents of information_schema and they see everything you have access to.

If you need to list all tables and columns in Redshift or Postgres, this guide gives you a SQL script that lists every schema, table and column in the database you are connected to.

Redshift is based on (forked from) PostgreSQL, meaning many core SQL commands work similarly in both. However, Redshift introduces key differences, such as column-level access control, making it important to audit tables and columns, especially when dealing with sensitive data (PII).

If you are looking to apply column-level access control in Postgres, the usual way is to create views that leave out the columns you want to hide, and grant SELECT on the views instead of the tables.

Script to Show all Schemas, Tables & Columns

The SQL query below retrieves all schemas, tables, and columns from Amazon Redshift or PostgreSQL databases:

SQL
-- Redshift list all schemas, tables and columns
select
	table_schema,
	table_name,
	ordinal_position as position,
	column_name,
	data_type,
	case when character_maximum_length is not null
		then character_maximum_length
		else numeric_precision end as max_length,
	is_nullable,
	column_default as default_value
from information_schema.columns
where table_schema not in ('information_schema','pg_catalog')
order by table_schema, table_name, ordinal_position;
Dbeaver Schema Table Columns List

The system schemas are already filtered out. To look at one schema only, change the WHERE clause:

SQL
-- one schema only
where table_schema = 'your_schema_name'

List Every Table

SQL
-- every table you can see, excluding the system schemas
SELECT table_schema, table_name, table_type
FROM   svv_tables
WHERE  table_schema NOT IN ('pg_catalog','information_schema')
ORDER  BY table_schema, table_name;

List Columns for a Table

SQL
SELECT table_schema, table_name, column_name, data_type, character_maximum_length
FROM   svv_columns
WHERE  table_schema = 'public'
AND    table_name   = 'orders'
ORDER  BY ordinal_position;

Find a Column Anywhere

The query I actually reach for most, when you know a column name but not where it lives:

SQL
SELECT table_schema, table_name, column_name, data_type
FROM   svv_columns
WHERE  column_name ILIKE '%customer_id%'
AND    table_schema NOT IN ('pg_catalog','information_schema')
ORDER  BY table_schema, table_name;

Table Sizes and Row Counts

SQL
-- size on disk per table
SELECT "schema" AS table_schema, "table" AS table_name, size AS size_mb, tbl_rows
FROM   svv_table_info
ORDER  BY size DESC
LIMIT  20;
SVV views only show what your user can access. If a table you expect is missing, it is usually permissions rather than a missing table. SVV_TABLE_INFO in particular returns nothing for tables you have no rights on, which makes a permissions problem look like an empty database.

The Postgres Equivalents

Redshift is built on PostgreSQL, so the standard views work too and are worth knowing if you move between the two:

SQL
-- works on Redshift and PostgreSQL
SELECT table_schema, table_name
FROM   information_schema.tables
WHERE  table_type = 'BASE TABLE'
AND    table_schema NOT IN ('pg_catalog','information_schema');

-- psql only: a client command, not SQL
\dt *.*

The difference worth remembering: SVV_TABLE_INFO is Redshift-specific and gives you size, distribution style and sort keys, which the standard views cannot.


Related

If SQL Server is part of your day job, my current work lives over at sqldba.blog: production DBA scripts, an error library and the SSMS guide.

Comments

One response to “List All Tables and Columns in Redshift or Postgres”

  1. Quintin White Avatar
    Quintin White

    Thank you, this is exactly what I needed !