Is it possible to rename multiple columns in a single statement, something along the lines of:
ALTER TABLE Users RENAME COLUMN userName TO user_name, RENAME COLUMN realName TO real_name;
First, specify the table, which contains the column you want to rename, after the ALTER TABLE clause. Second, provide the column name after the RENAME COLUMN clause. Third, give the new column name after the TO keyword.
The syntax to rename a column in a table in PostgreSQL (using the ALTER TABLE statement) is: ALTER TABLE table_name RENAME COLUMN old_name TO new_name; table_name.
No.
While other actions can be combined, that's not possible with RENAME
. The manual:
All the forms of
ALTER TABLE
that act on a single table, exceptRENAME
,SET SCHEMA
,ATTACH PARTITION
, andDETACH PARTITION
can be combined into a list of multiple alterations to be applied together.
Since RENAME
is a tiny operation on a system catalog, there is no harm in running multiple statements. Do it in a single transaction to minimize locking overhead and avoid race conditions with intermediate states (which are then invisible to all other transactions).
Other actions like ALTER COLUMN ... SET TYPE
are potentially expensive because they may have to rewrite the whole table. With big tables it would be wise to do as much as possible in a single statement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With