2012-11-22 25 views

回答

4

您需要查詢pg_cataloginformation_schema模式下的相應表格。 運行psql -E ...然後PSQL顯示在內部命令中使用像\d, \dt, \dv, ...

information_schema元數據查詢是相同pg_catalog但更portalbe(它是在SQL標準中定義)。如果應用程序使用Postgres的只有我會使用,而不是information_schema

例如pg_catalog,這個查詢顯示列attribute

SELECT a.attname, 
    pg_catalog.format_type(a.atttypid, a.atttypmod), 
    (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128) 
    FROM pg_catalog.pg_attrdef d 
    WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef), 
    a.attnotnull, a.attnum 
FROM pg_catalog.pg_attribute a 
WHERE a.attrelid = 'attribute'::regclass AND a.attnum > 0 AND NOT a.attisdropped 
ORDER BY a.attnum 

UPDATE: 您可以使用簡單的查詢是這樣的:

SELECT attname 
FROM pg_catalog.pg_attribute 
WHERE attrelid = 'attribute'::regclass AND attnum > 0 AND NOT attisdropped 

或使用同等查詢:

SELECT column_name 
FROM information_schema.columns 
WHERE table_name = 'attribute' 
ORDER BY ordinal_position 

如果在多個模式中具有相同名稱的同一表,則還需要用戶模式名稱,查詢會稍微複雜一點。

+1

非常感謝您的回答。不過,我還是不太明白。您發佈的查詢在'屬性'附近顯示錯誤。我試過「從pg_catalog.pg_attribute選擇attname;」但是,它顯示的不僅僅是我需要的。 –

+0

對不起,'屬性'是我的示例表名:)。什麼是錯誤信息?這是來自Postgres 8.4 - 也許是一些複製和粘貼格式問題。如果你有名爲'my_table'的表,那麼使用'... WHERE a.attrelid ='my_table':: regclass AND ...' – mys

相關問題