6

Is there a way I can rename all column names that are called 'location' to 'location_name' within all schemas in my PostgreSQL database?

I am fairly new to SQL and am aware there is an ALTER TABLE command but don't know if it is possible to somehow loop through all tables?

2
  • 1
    Always have a script to re-create a database from scratch. Edit that script. Commented Aug 23, 2016 at 8:03
  • Better fit Database Administrators Commented May 28, 2018 at 22:12

5 Answers 5

8

You need dynamic SQL for this using an anonymous PL/pgSQL block to do this in an automated way:

do
$$
declare
  l_rec record;
begin
  for l_rec in (select table_schema, table_name, column_name 
                from information_schema.columns 
                where table_schema = 'public' 
                  and column_name = 'location') loop
     execute format ('alter table %I.%I rename column %I to %I_name', l_rec.table_schema, l_rec.table_name, l_rec.column_name, l_rec.column_name);
  end loop;
end;
$$

If you have multiple schemas or your tables are not in the public schema, replace the condition where table_schema = 'public' with the appropriate restriction.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your detailed response, I was successful with Patrick's reply but I am sure this would work as well.
@Chris: messing around with the system catalogs is really not a good solution.
5

If you have superuser privileges you can make the changes in one sweep in the system catalogs:

UPDATE pg_attribute
SET attname = 'location_name'
WHERE attname = 'location';

1 Comment

Wow, this is a terrible idea. You should not be modifying the catalog directly.
2

I think the closest you will get is running something like this:

SELECT FORMAT(
  'ALTER TABLE %I.%I.%I RENAME COLUMN %I TO %I;',
  catalog_name,
  schema_name,
  table_name,
  column_name,
  'location_name'
)
FROM information_schema.columns
WHERE column_name = 'location';

If you run that in psql, you can follow it with \gexec and it should work.

Comments

1

You can fetch all location columns in your database with

SELECT * FROM information_schema.columns WHERE column_name = 'location';

And probably use some programming language to then execute all rename queries. There are information_schema tables in almost every database management system (although, they are sometimes called/structured differently).

Comments

0

I just had the same problem, and the following code worked fine for me (just replace 'foo' and 'bar' with your old and new column names):

DO $$ 
DECLARE 
    l_rec record; 
BEGIN 
    FOR l_rec IN (SELECT table_schema, table_name, column_name 
                  FROM information_schema.columns 
                  WHERE column_name = 'foo') 
    LOOP 
        EXECUTE FORMAT('ALTER TABLE %I.%I RENAME COLUMN %I TO %I', 
                       l_rec.table_schema, l_rec.table_name, 
                       l_rec.column_name, 'bar'); 
    END LOOP; 
END; 
$$;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.