HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlCritical

How can I specify the position for a new column in PostgreSQL?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlcanthenewcolumnpositionforhowspecify

Problem

If I have a table with the columns:

id | name | created_date


and would like to add a column, I use:

alter table my_table add column email varchar(255)


Then the column is added after the created_date column.

Is there any way I can specify the position for the new column? e.g. so I can add it after name and get a table like:

id | name | email | created_date

Solution

ALTER TABLE ADD COLUMN will only add the new column at the end, as the last one.
In order to create a new column in another position you need to recreate the table and copy the data from the old/current table in this new table.

Context

StackExchange Database Administrators Q#3276, answer score: 113

Revisions (0)

No revisions yet.