There is no single aproach command. Obviously, you could enter some commands for RENAME by yourself, but let me make some improvements :) As I said in this answer
... for all such bulk-admin operations, you can use PostgreSQL system tables to generate queries for you, rather than manually writing them
In your case, it will be:
SELECT 'ALTER TABLE ' || tab_name || ' RENAME COLUMN ' || quote_ident(column_name) || ' TO ' || quote_ident( '_1' || column_name) || ';' FROM ( SELECT quote_ident(table_schema) || '.' || quote_ident(table_name) as tab_name, column_name FROM information_schema.columns WHERE table_schema = 'schema_name' AND table_name = 'table_name' AND column_name LIKE '\_%' ) sub;
This will give you a set of strings that are SQL commands, such as:
ALTER TABLE schema_name.table_name RENAME COLUMN "_settingA" TO "_1_settingA"; ALTER TABLE schema_name.table_name RENAME COLUMN "_settingB" TO "_1_settingB"; ...
There is no need to use table_schema in WHERE if your table is in the public schema. Also remember, using the quote_ident() function - read my original answer for more explanation.
Edit:
I changed my query so that it now works for all columns with a name starting with an underscore _ . Since the underscore is a special character in SQL pattern matching, we must avoid it (using \ ) to find it.
source share