我如何将Postgres DDL转储到可粘贴到Google表格中的CSV中?

问题描述 投票:0回答:1

我正在尝试从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;
database postgresql ddl
1个回答
0
投票

我想我对此有点考虑过。这是我最终使用的最终SQL:

© www.soinside.com 2019 - 2024. All rights reserved.