[postgis-users] Removed double quotes from column and table names

Mike Toews mwtoews at gmail.com
Tue Mar 20 16:09:34 PDT 2012


On 21 March 2012 11:52, Simon Greener <simon at spatialdbadvisor.com> wrote:
> Thanks all for the suggestions.
> S

If you are using postgres 9.0 or later, try something like this to
rename tables:

DO $$DECLARE r record;
BEGIN
    FOR r IN (SELECT relname,
                regexp_replace(lower(relname), E'[ \-]+', '_', 'g') AS
new_relname
              FROM pg_class c
              JOIN pg_namespace n ON n.oid=c.relnamespace
              WHERE n.nspname='public' AND relkind='r'
                AND relname <> quote_ident(relname))
    LOOP
        EXECUTE 'ALTER TABLE ' || quote_ident(r.relname) ||
          ' RENAME TO ' || quote_ident(r.new_relname) || ';';
    END LOOP;
END$$

A similar query can be used to rename columns.

-Mike



More information about the postgis-users mailing list