Show tables, describe tables equivalent in redshift
Asked Answered
A

10

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
Astereognosis answered 11/9, 2013 at 5:36 Comment(0)
K
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;
Keelboat answered 11/9, 2013 at 11:37 Comment(8)
Is there way to fetch default values set of respective columns ?Vie
pg_table_def returns index also. safer to use select distinct table_name from information_schema.columns where table_schema = 'public' instead.Elvieelvin
is that in column order ?Gird
see alberton.info/postgresql_meta_info.html for a bunch of simpler ways to extract column info, including orderGird
Maybe they used work to at the time of this answer, but these queries don't return all tables anymore (2018-07-11). For example, I created my own schema and created a table in that schema, and the pg_table_def table doesn't have any information about that table. The query suggested by @Elvieelvin -- to query information_schema instead does work.Hardigg
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'Combinative
This did not show some tables that \d+ <table name> below did.Damascene
what about temp tables? I want to describe a temp table I created but temp tables cant be created with schemaMethinks
C
89

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'; 
Cantone answered 7/1, 2015 at 16:56 Comment(4)
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!Microelement
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.Finke
@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...Scutt
@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.Pinnule
M
33

Or simply:

\dt to show tables

\d+ <table name> to describe a table

Edit: Works using the psql command line client

Merlynmermaid answered 12/9, 2013 at 22:54 Comment(5)
This does not work for me .. it would work in Posgres but not Redshift .. better to go with Tomasz' answer.Demilitarize
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?Demilitarize
Yes, using psql command line client. I will update my answer.Merlynmermaid
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.Demilitarize
It shows slightly different information than Tomasz's solution. For example, \d+ tablename does not show sortkey information.Nardoo
L
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
Licking answered 16/6, 2017 at 10:20 Comment(2)
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.Orten
This is the correct solution concerned with SEARCH_PATH. But I only need to set search_path to my_schema_name.Pinnule
I
2

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>>';
Ironmaster answered 16/8, 2023 at 2:2 Comment(0)
A
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.

Agrarian answered 11/4, 2015 at 8:5 Comment(1)
thanks for that! Still missing a way of listing tables :/Drier
I
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;
Intrigue answered 29/4, 2017 at 16:2 Comment(0)
M
1

Shortcut

\d for show all tables

\d tablename to describe table

\? for more shortcuts for redshift

Manlove answered 28/2, 2020 at 5:37 Comment(0)
C
1

redshift now support show table

show table analytics.dw_users

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

Cotenant answered 13/5, 2021 at 3:32 Comment(0)
G
-7

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

desc table-name

or

desc schema-name.table-name
Gilliam answered 6/4, 2017 at 18:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.