1
是否有可能對postgresql中的某個類型的所有列進行批量重命名。我有一系列帶有「the_geom」,「geom」,「SP_GEOMETRY」等名稱的幾何表格,每個表格都有幾何類型列(每個表只有1個表格),由於使用了不同的導入工具,它們都有不同的名稱。大量重命名列postgresql
我希望能夠將它們全部重命名爲「the_geom」或「geom」。
是否有可能對postgresql中的某個類型的所有列進行批量重命名。我有一系列帶有「the_geom」,「geom」,「SP_GEOMETRY」等名稱的幾何表格,每個表格都有幾何類型列(每個表只有1個表格),由於使用了不同的導入工具,它們都有不同的名稱。大量重命名列postgresql
我希望能夠將它們全部重命名爲「the_geom」或「geom」。
查詢系統目錄並生成命令通常是最簡單的方法。事情是這樣的:
select
'alter table ' || quote_ident(nspname) || '.' || quote_ident(relname)
|| ' rename column ' || quote_ident(attname)
|| ' to ' || quote_ident('xxx')
from pg_attribute
join pg_class on pg_class.oid = pg_attribute.attrelid
join pg_namespace on pg_namespace.oid = pg_class.relnamespace
where atttypid = 'geometry'::regtype and (attname ~* 'geom')
and not attisdropped and attnum > 0 and pg_class.oid >= 16384
運行此查詢生成所需的所有DDL語句:
SELECT 'ALTER TABLE ' || quote_ident(n.nspname) || '.' || quote_ident(c.relname)
|| ' RENAME column ' || quote_ident(a.attname) || ' TO geom;'
FROM pg_catalog.pg_attribute a
JOIN pg_catalog.pg_class c ON c.oid = a.attrelid
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE a.attnum >= 1
AND a.atttypid = 'geometry'::regtype::oid
AND a.attname <> 'geom'
AND NOT a.attisdropped
AND n.nspname !~~ 'pg_%' -- exclude catalog & temp tables, to be sure
-- AND n.nspname = 'myschema' -- target specific schema only?
哪個版本是你嗎? – Kuberchaun
我在版本8.4 –