2014-04-30 20 views
7

Ciao Sto cercando di recuperare lo schema di una tabella esistente. Sono uno sviluppatore mysql e sto cercando di lavorare con Amazon Redshift. Come posso esportare lo schema di una tabella esistente. In mysql possiamo usare il comando show create table.Ottieni schema tabella in Redshift

SHOW CREATE TABLE tblName; 

risposta

5

Se si desidera ottenere la struttura della tabella con CREATE, i vincoli e trigger, è possibile utilizzare l'utilità pg_dump uscita

pg_dump -U user_name -s -t table_name -d db_name 
Note: -s used for schema only dump 
if you want to take the data only dump , you can use -a switch. 

Questa volontà di creare la sintassi con tutti i vincoli. Spero che questo ti possa aiutare.

+2

Sembra che per Postgres 7.3+ bandiera '-d' scomparve dalla' pg_dump', e il database è un argomento normale riga di comando. Cioè: 'pg_dump -U nome_utente -s -t nome_tabella nome_db'. – congusbongus

0

È necessario recuperarlo in modo programmatico o dal prompt di psql?

In uso psql: \ d + nometabella

Programatically, è possibile interrogare il viste INFORMATION_SCHEMA standard ANSI documentati qui:

http://www.postgresql.org/docs/9.1/static/information-schema.html 

L'INFORMATION_SCHEMA.TABLES e vista INFORMATION_SCHEMA.COLUMNS dovrebbero avere quello che ti serve.

1

In Postgres, devi interrogare il catalogo.

Da psql utilizzare le combinazioni di tasti per una serie di comandi di cui si otterrà l'elenco utilizzando \? (per informazioni). Per questo, uno dei due:

\d yourtable 
\d+ yourtable 

Per l'utilizzo in un'app, è necessario conoscere le query pertinenti coinvolte. È relativamente semplice eseguendo psql -E (per le query nascoste di eco) invece del semplice psql.

Se è necessaria l'istruzione della tabella di creazione precisa, consultare la risposta di @Anant.

5

Recentemente ho scritto uno script python per clonare schemi di tabelle tra i cluster redshift. Se si desidera solo le colonne ei tipi di colonne di una tabella, è possibile farlo tramite:

select column_name, 
    case 
    when data_type = 'integer' then 'integer' 
    when data_type = 'bigint' then 'bigint' 
    when data_type = 'smallint' then 'smallint' 
    when data_type = 'text' then 'text' 
    when data_type = 'date' then 'date' 
    when data_type = 'real' then 'real' 
    when data_type = 'boolean' then 'boolean' 
    when data_type = 'double precision' then 'float8' 
    when data_type = 'timestamp without time zone' then 'timestamp' 
    when data_type = 'character' then 'char('||character_maximum_length||')' 
    when data_type = 'character varying' then 'varchar('||character_maximum_length||')' 
    when data_type = 'numeric' then 'numeric('||numeric_precision||','||numeric_scale||')' 
    else 'unknown' 
    end as data_type, 
    is_nullable, 
    column_default 
from information_schema.columns 
where table_schema = 'xxx' and table_name = 'xxx' order by ordinal_position 
; 

Ma se avete bisogno i tipi di compressione e distkey/sortkeys, è necessario interrogare un altro tavolo:

select * from pg_table_def where tablename = 'xxx' and schemaname='xxx'; 
9

Questa query fornisce la definizione dello schema completa che include il tipo/chiave di distribuzione degli attributi specifici di Redshift, la chiave di ordinamento, la chiave primaria e le codifiche delle colonne sotto forma di un'istruzione create oltre a fornire un'istruzione alter table che imposta il proprietario a l'attuale proprietario. L'unica cosa che non può dirti sono le chiavi straniere. Sto lavorando su quest'ultimo, ma c'è un problema di privilegi corrente in RS che ci impedisce di interrogare le tabelle giuste. Questa query potrebbe utilizzare un po 'di ottimizzazione, ma non ho avuto il tempo o la necessità di lavorare ulteriormente.

select pk.pkey, tm.schemaname||'.'||tm.tablename, 'create table '||tm.schemaname||'.'||tm.tablename 
||' (' 
||cp.coldef 
-- primary key 
||decode(pk.pkey,null,'',pk.pkey) 
-- diststyle and dist key 
||decode(d.distkey,null,') diststyle '||dist_style||' ',d.distkey) 
--sort key 
|| (select decode(skey,null,'',skey) from (select 
' sortkey(' ||substr(array_to_string(
       array(select ','||cast(column_name as varchar(100)) as str from 
         (select column_name from information_schema.columns col where col.table_schema= tm.schemaname and col.table_name=tm.tablename) c2 
         join 
         (-- gives sort cols 
          select attrelid as tableid, attname as colname, attsortkeyord as sort_col_order from pg_attribute pa where 
          pa.attnum > 0 AND NOT pa.attisdropped AND pa.attsortkeyord > 0 
         ) st on tm.tableid=st.tableid and c2.column_name=st.colname order by sort_col_order 
        ) 
       ,'') 
       ,2,10000) || ')' as skey 
)) 
||';' 
-- additional alter table queries here to set owner 
|| 'alter table '||tm.schemaname||'.'||tm.tablename||' owner to "'||tm.owner||'";' 
from 
-- t master table list 
(
SELECT substring(n.nspname,1,100) as schemaname, substring(c.relname,1,100) as tablename, c.oid as tableid ,use2.usename as owner, decode(c.reldiststyle,0,'EVEN',1,'KEY',8,'ALL') as dist_style 
FROM pg_namespace n, pg_class c, pg_user use2 
WHERE n.oid = c.relnamespace 
AND nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema') 
AND c.relname <> 'temp_staging_tables_1' 
and c.relowner = use2.usesysid 
) tm 
-- cp creates the col params for the create string 
join 
(select 
substr(str,(charindex('QQQ',str)+3),(charindex('ZZZ',str))-(charindex('QQQ',str)+3)) as tableid 
,substr(replace(replace(str,'ZZZ',''),'QQQ'||substr(str,(charindex('QQQ',str)+3),(charindex('ZZZ',str))-(charindex('QQQ',str)+3)),''),2,10000) as coldef 
from 
(select array_to_string(array(
SELECT 'QQQ'||cast(t.tableid as varchar(10))||'ZZZ'|| ','||column_name||' '|| decode(udt_name,'bpchar','char',udt_name) || decode(character_maximum_length,null,'', '('||cast(character_maximum_length as varchar(9))||')' ) 
-- default 
|| decode(substr(column_default,2,8),'identity','',null,'',' default '||column_default||' ') 
-- nullable 
|| decode(is_nullable,'YES',' NULL ','NO',' NOT NULL ') 
-- identity 
|| decode(substr(column_default,2,8),'identity',' identity('||substr(column_default,(charindex('''',column_default)+1), (length(column_default)-charindex('''',reverse(column_default))-charindex('''',column_default) )) ||') ', '') 
-- encoding 
|| decode(enc,'none','',' encode '||enc) 
as str 
from 
-- ci all the col info 
(
select cast(t.tableid as int), cast(table_schema as varchar(100)), cast(table_name as varchar(100)), cast(column_name as varchar(100)), 
cast(ordinal_position as int), cast(column_default as varchar(100)), cast(is_nullable as varchar(20)) , cast(udt_name as varchar(50)) ,cast(character_maximum_length as int), 
sort_col_order , decode(d.colname,null,0,1) dist_key , e.enc 
from 
(select * from information_schema.columns c where c.table_schema= t.schemaname and c.table_name=t.tablename) c 
left join 
(-- gives sort cols 
select attrelid as tableid, attname as colname, attsortkeyord as sort_col_order from pg_attribute a where 
a.attnum > 0 AND NOT a.attisdropped AND a.attsortkeyord > 0 
) s on t.tableid=s.tableid and c.column_name=s.colname 
left join 
(-- gives encoding 
select attrelid as tableid, attname as colname, format_encoding(a.attencodingtype::integer) AS enc from pg_attribute a where 
a.attnum > 0 AND NOT a.attisdropped 
) e on t.tableid=e.tableid and c.column_name=e.colname 
left join 
-- gives dist col 
(select attrelid as tableid, attname as colname from pg_attribute a where 
a.attnum > 0 AND NOT a.attisdropped AND a.attisdistkey = 't' 
) d on t.tableid=d.tableid and c.column_name=d.colname 
order by ordinal_position 
) ci 
-- for the working array funct 
), '') as str 
from 
(-- need tableid 
SELECT substring(n.nspname,1,100) as schemaname, substring(c.relname,1,100) as tablename, c.oid as tableid 
FROM pg_namespace n, pg_class c 
WHERE n.oid = c.relnamespace 
AND nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema') 
) t 
)) cp on tm.tableid=cp.tableid 
-- primary key query here 
left join 
(select c.oid as tableid, ', primary key '|| substring(pg_get_indexdef(indexrelid),charindex('(',pg_get_indexdef(indexrelid))-1 ,60) as pkey 
from pg_index i , pg_namespace n, pg_class c 
where i.indisprimary=true 
and i.indrelid =c.oid 
and n.oid = c.relnamespace 
) pk on tm.tableid=pk.tableid 
-- dist key 
left join 
( select 
-- close off the col defs after the primary key 
')' || 
' distkey('|| cast(column_name as varchar(100)) ||')' as distkey, t.tableid 
from information_schema.columns c 
join 
(-- need tableid 
SELECT substring(n.nspname,1,100) as schemaname, substring(c.relname,1,100) as tablename, c.oid as tableid 
FROM pg_namespace n, pg_class c 
WHERE n.oid = c.relnamespace 
AND nspname NOT IN ('pg_catalog', 'pg_toast', 'information_schema') 
) t on c.table_schema= t.schemaname and c.table_name=t.tablename 
join 
-- gives dist col 
(select attrelid as tableid, attname as colname from pg_attribute a where 
a.attnum > 0 AND NOT a.attisdropped AND a.attisdistkey = 't' 
) d on t.tableid=d.tableid and c.column_name=d.colname 

) d on tm.tableid=d.tableid 
where tm.schemaname||'.'||tm.tablename='myschema.mytable' 
+2

AWS ha rilasciato questa fantastica utility, https://github.com/awslabs/amazon-redshift-utils, per la migrazione alla codifica di compressione corretta. Contiene tutte le query utilizzate da AWS per descrivere le strutture di tabelle con l'unica eccezione delle concessioni (vedere http://stackoverflow.com/questions/18741334/how-do-i-view-grants-on-redshift/21178589#21178589 per quello componente). –

0

Il comando di seguito funzionerà:

mysql > show create table test.users_info; 

Redshift/postgress >pg_dump -U root-w --no-password -h 62.36.11.547 -p 5439 -s -t test.users_info ; 
Problemi correlati