Re: How to rename the same column name in dozens of tables in Postgres?

From: Raghavendra <raghavendra(dot)rao(at)enterprisedb(dot)com>
To: Stefan Schwarzer <stefan(dot)schwarzer(at)unep(dot)org>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to rename the same column name in dozens of tables in Postgres?
Date: 2013-12-17 08:53:10
Message-ID: CA+h6AhhUoFu2z7ZirbDpuCE-X_FKE57m59GWB0zBFdqPLqRJCQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Dec 17, 2013 at 1:29 PM, Stefan Schwarzer <stefan(dot)schwarzer(at)unep(dot)org
> wrote:

> Hi there,
>
> I have dozens of tables where I need to rename one column in each of
> these. Is there any generic way to do that?
>
I am not really familiar with the scripting possibilities of Postgres.
>
> Thanks a lot for your help!
>
> Stefan
>
>
You can do with script. I made one on fly for this, other's might have more
polished version.

-bash-4.1$ more rename.sh
#!/bin/bash
OLDCOLUMN=aa
NEWCOLUMN=a
for i in $(psql -t -c "select table_schema||'.'||table_name from
information_schema.tables where table_schema='public';")
do
/opt/PostgreSQL/9.3/bin/psql -p 5432 -U postgres -c "alter table $i
rename column $OLDCOLUMN to $NEWCOLUMN;"
done

Replace the port,user,OLDCOLUMN, NEWCOLUMN and SCHEMA according to your
requirement.

---
Regards,
Raghavendra
EnterpriseDB Corporation
Blog: http://raghavt.blogspot.com/

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Raghavendra 2013-12-17 09:04:59 Re: How to rename the same column name in dozens of tables in Postgres?
Previous Message Rahila Syed 2013-12-17 08:49:10 Re: Replication: GZIP compression in WAL sender/receiver processes communication?