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
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
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;
select distinct table_name from information_schema.columns where table_schema = 'public'
instead. –
Elvieelvin 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 set search_path to my_schema
and consequently update the query to filter on schemaname = 'my_schema'
–
Combinative \d+ <table name>
below did. –
Damascene 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';
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 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 Or simply:
\dt
to show tables
\d+ <table name>
to describe a table
Edit: Works using the psql command line client
\dt
I get Error: Syntax error at or near "\"
. Are you accessing Redshift in some other way? –
Demilitarize 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
SEARCH_PATH
. But I only need to set search_path to my_schema_name
. –
Pinnule 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>>';
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.
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;
Shortcut
\d for show all tables
\d tablename to describe table
\? for more shortcuts for redshift
redshift now support show table
show table analytics.dw_users
You can simply use the command below to describe a table.
desc table-name
or
desc schema-name.table-name
© 2022 - 2024 — McMap. All rights reserved.