我正在尝试从Postgres创建输出,该输出将执行以下操作:
schema, table, column, type, attributes
public, table1, id, bigserial, not null primary key
public, table1, name, string, not null
...
只要输出使用列标题设置格式,如果不是CSV,我就可以了(我可以将普通查询导出为CSV就可以了。我无法正确查询周围的内容。
我最近来的如下。但是感觉就像我走错了路,并且有一种更简单/更干净的方式来做到这一点。我现在也没有在每个表列中得到一行(这是我想要的)。
SELECT table_schema || ',' || table_name || ',' ||
string_agg(column_list.column_expr, ';' || '') ||
'' || ');'
FROM (
SELECT table_schema, table_name, ' ' || column_name || ',' || data_type ||
coalesce('(' || character_maximum_length || ')', ', ') ||
case when is_nullable = 'YES' then '' else ' NOT NULL' end as column_expr
FROM information_schema.columns
WHERE table_schema = 'public'
ORDER BY ordinal_position) column_list
group by table_schema,table_name;
我想我对此有点考虑过。这是我最终使用的最终SQL: