How do I add a new column between 2 columns in Postgresql?

Asked

Viewed 2,293 times

3

How do I add a new column between two existing columns in my Postgresql database?

In Mysql I use the function AFTER to add one column after another, and in Postgresql? How do I do this?

SQL

ALTER TABLE nota ADD hota_utc VARCHAR(2) AFTER hora_entrada_saida;
  • 1

    The AFTER is a Mysql-only instruction.

1 answer

4


It is not possible. The solution is to create a new table with the new structure with the columns the way you want, copy the data to it and after deleting the old, rename the new to take the place of the old.

The order of columns is generally considered irrelevant.

There is an explanation of how to proceed in the product wiki.

  • Okay @bigown. Thank you very much!

  • In fact it is possible, I already researched on the subject in English, and I found answers that mentioned being possible by changing the internal counters in the management core of postgres, in information_schema in the desired table, however, the management for this is so great that it is not recommended to do, besides in case of error could generate complications not only in the current table but in other unwanted ones. In conclusion, it is possible, but it is better to do as @bigown said.

  • It is not possible in a natural way. Gambiarra can always do.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.