88

I'm new to aws, can anyone tell me what are redshifts' equivalents to mysql commands?

show tables -- redshift command
describe table_name -- redshift command

10 Answers 10

125

All the information can be found in a PG_TABLE_DEF table, documentation.

Listing all tables in a public schema (default) - show tables equivalent:

SELECT DISTINCT tablename
FROM pg_table_def
WHERE schemaname = 'public'
ORDER BY tablename;

Description of all the columns from a table called table_name - describe table equivalent:

SELECT *
FROM pg_table_def
WHERE tablename = 'table_name'
AND schemaname = 'public';

Update:

As pointed by @Kishan Pandey 's answer, if you are looking for details of a schema different by public, you need to set search_path to my_schema. (show search_path display current search path)

Listing tables in my_schema schema:

set search_path to my_schema;
select * from pg_table_def;
8
  • 1
    Is there way to fetch default values set of respective columns ? Commented Dec 29, 2014 at 12:59
  • 4
    pg_table_def returns index also. safer to use select distinct table_name from information_schema.columns where table_schema = 'public' instead.
    – j_c
    Commented Jan 27, 2015 at 23:19
  • is that in column order ?
    – abourget
    Commented Apr 20, 2015 at 21:29
  • see alberton.info/postgresql_meta_info.html for a bunch of simpler ways to extract column info, including order
    – abourget
    Commented Apr 21, 2015 at 14:43
  • 1
    as pointed by @Kishan Pandey 's answer, if you are looking for details of a schema different by public, you need to set search_path to my_schema and consequently update the query to filter on schemaname = 'my_schema'
    – Vzzarr
    Commented Nov 5, 2019 at 13:07
90

I had to select from the information schema to get details of my tables and columns; in case it helps anyone:

SELECT * FROM information_schema.tables
WHERE table_schema = 'myschema'; 

SELECT * FROM information_schema.columns
WHERE table_schema = 'myschema' AND table_name = 'mytable'; 
4
  • 1
    I am storing all our Segment SQL data to AWS Redshift, and so far this is the only solution I have come across that has worked for me!
    – mgig
    Commented Mar 28, 2016 at 21:53
  • 2
    I find this the superior method. The information_schema in Redshift is a superset of pg_table. Furthermore, pg_table_def seems to suffer from some kind of permission issue: while a schema/table will be in pg_tables, the corresponding information in pg_table_def will NOT be there. All I get is pg_catalog and public schemae. Frustrating. Commented Aug 8, 2017 at 20:08
  • 4
    @MarkGerolimatos To see your tables in pg_table_def, you must add the schema to your SEARCH_PATH: SET SEARCH_PATH to '$user', public, YOUR_SCEHMA_NAME. Definitely not intuitive...
    – Marco Roy
    Commented Dec 11, 2017 at 19:05
  • 1
    @MarcoRoy Worth mentiong that, the pg_table_def is more informative as I see from the query results. pg_table_def shows whether the column is distkey or sortkey and its encoding, which are not shown in the information_schema.columns. I guess that's where SQL clients like DataGrip gets the DDL from.
    – Rick
    Commented Apr 15, 2021 at 6:52
33

Or simply:

\dt to show tables

\d+ <table name> to describe a table

Edit: Works using the psql command line client

5
  • This does not work for me .. it would work in Posgres but not Redshift .. better to go with Tomasz' answer. Commented Nov 24, 2014 at 7:43
  • 1
    I am accessing Redshift via SQLWorkbench (as recommended in the AWS getting started guide). If I try \dt I get Error: Syntax error at or near "\". Are you accessing Redshift in some other way? Commented Nov 26, 2014 at 1:25
  • 2
    Yes, using psql command line client. I will update my answer.
    – Matt
    Commented Nov 26, 2014 at 22:51
  • 2
    Actually I didn't realise you could connect to Redshift with the psql client, but it is in the Amazon documentation. It's actually very useful. Thanks. Commented Dec 3, 2014 at 11:22
  • It shows slightly different information than Tomasz's solution. For example, \d+ tablename does not show sortkey information.
    – Kirk
    Commented Dec 30, 2014 at 18:06
12

Tomasz Tybulewicz answer is good way to go.

SELECT * FROM pg_table_def WHERE tablename = 'YOUR_TABLE_NAME' AND schemaname = 'YOUR_SCHEMA_NAME';

If schema name is not defined in search path , that query will show empty result. Please first check search path by below code.

SHOW SEARCH_PATH

If schema name is not defined in search path , you can reset search path.

SET SEARCH_PATH to '$user', public, YOUR_SCEHMA_NAME
2
  • 1
    You may want to add the search path comment to Tomasz's answer. I had this issue the first time I used the pg views and had to research it.
    – Rob
    Commented Nov 9, 2017 at 16:02
  • This is the correct solution concerned with SEARCH_PATH. But I only need to set search_path to my_schema_name.
    – Rick
    Commented Apr 15, 2021 at 6:36
2
Answer recommended by AWS Collective

Redshift provides system tables and views which are simpler to use than the postgres default pg_catalog and information_schema tables. You can find the list of tables here

To get list of tables in a schema, you can query SVV_ALL_TABLES / SVV_REDSHIFT_TABLES

    select * from SVV_REDSHIFT_TABLES where schema_name = '<<schema_name>>';

To get table definition including is sort key and distribution key, you can query SVV_REDSHIFT_COLUMNS

    select * from SVV_REDSHIFT_COLUMNS where schema_name = '<<schema_name>>' and table_name = '<<table_name>>';
1

You can use - desc / to see the view/table definition in Redshift. I have been using Workbench/J as a SQL client for Redshift and it gives the definition in the Messages tab adjacent to Result tab.

1
  • 1
    thanks for that! Still missing a way of listing tables :/ Commented May 10, 2016 at 9:48
1

In the following post, I documented queries to retrieve TABLE and COLUMN comments from Redshift. https://sqlsylvia.wordpress.com/2017/04/29/redshift-comment-views-documenting-data/

Enjoy!

Table Comments

    SELECT n.nspname AS schema_name
     , pg_get_userbyid(c.relowner) AS table_owner
     , c.relname AS table_name
     , CASE WHEN c.relkind = 'v' THEN 'view' ELSE 'table' END 
       AS table_type
     , d.description AS table_description
     FROM pg_class As c
     LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
     LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
     LEFT JOIN pg_description As d 
          ON (d.objoid = c.oid AND d.objsubid = 0)
     WHERE c.relkind IN('r', 'v') AND d.description > ''
     ORDER BY n.nspname, c.relname ;

Column Comments

    SELECT n.nspname AS schema_name
     , pg_get_userbyid(c.relowner) AS table_owner
     , c.relname AS table_name
     , a.attname AS column_name
     , d.description AS column_description
    FROM pg_class AS c
    INNER JOIN pg_attribute As a ON c.oid = a.attrelid
    INNER JOIN pg_namespace n ON n.oid = c.relnamespace
    LEFT JOIN pg_tablespace t ON t.oid = c.reltablespace
    LEFT JOIN pg_description As d 
     ON (d.objoid = c.oid AND d.objsubid = a.attnum)
    WHERE c.relkind IN('r', 'v')
     AND a.attname NOT         
     IN ('cmax', 'oid', 'cmin', 'deletexid', 'ctid', 'tableoid','xmax', 'xmin', 'insertxid')
    ORDER BY n.nspname, c.relname, a.attname;
0
1

Shortcut

\d for show all tables

\d tablename to describe table

\? for more shortcuts for redshift

1

redshift now support show table

show table analytics.dw_users

https://forums.aws.amazon.com/ann.jspa?annID=8641

0
-7

You can simply use the command below to describe a table.

desc table-name

or

desc schema-name.table-name

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.