Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change sequence name in postgreSQL

Tags:

I would like to change the name of my sequence I use. I tried those two options and failed:

ALTER TABLE PLAYER RENAME id_seq_player TO player_id_seq; 

and

ALTER SEQUENCE id_seq_player RENAME TO player_id_seq; 

I looked at the official documentation and from there I created those two SQL, but none of them worked. The first solution resulted in a SQL error and the second had a time out.

Here is the SQL error I get:

ERROR:  column "id_seq_player" does not exist  ********** Error ********** 

* UPDATE *

Seems like the second SQL statement did the job. Since I have to forward the port, maybe it was an issue with the connection or OpenShift. But now I retried several times to verify and it works.

ALTER SEQUENCE id_seq_player RENAME TO player_id_seq; /* Works */ 
like image 684
Jernej K Avatar asked Apr 01 '16 16:04

Jernej K


People also ask

How do I rename a sequence in PostgreSQL?

ALTER SEQUENCE old_id_seq RENAME TO new_id_seq; ALTER TABLE mytable ALTER COLUMN id SET DEFAULT nextval('new_id_seq');

How do you alter a sequence?

Sequences objects are created by using the CREATE SEQUENCE statement. Sequences are integer values and can be of any data type that returns an integer. The data type cannot be changed by using the ALTER SEQUENCE statement. To change the data type, drop and create the sequence object.

How do I change the order of a column in PostgreSQL?

"Alter column position" in the PostgreSQL Wiki says: PostgreSQL currently defines column order based on the attnum column of the pg_attribute table. The only way to change column order is either by recreating the table, or by adding columns and rotating data until you reach the desired layout.


1 Answers

ALTER SEQUENCE id_seq_player RENAME TO player_id_seq; 

is correct. You might want to add the schema name as well to ensure you are altering the correct one, but this should most likely work.

If it timeouts, you might have another process that is locking your sequence. Is there a way for you to turn off all the other database users, or is it too critical to do so?

like image 78
Anthony Drogon Avatar answered Oct 08 '22 13:10

Anthony Drogon