如何在PostgreSQL中设置自动递增主键?
问题描述:
我在PostgreSQL中有一个具有22列的表,我想添加一个自动增量主键.
I have a table in PostgreSQL with 22 columns, and I want to add an auto increment primary key.
我试图创建一个名为BIGSERIAL类型的id
列,但是pgadmin响应了一个错误:
I tried to create a column called id
of type BIGSERIAL but pgadmin responded with an error:
ERROR: sequence must have same owner as table it is linked to.
有人知道如何解决此问题吗?如何在PostgreSQL中添加创建自动递增主键而又不重新创建表的方法?
Does anyone know how to fix this issue? How do I add a create an auto-incrementing primary key in PostgreSQL without recreating the table again?
答
尝试以下命令:
ALTER TABLE your_table ADD COLUMN key_column BIGSERIAL PRIMARY KEY;
请使用与您创建该表的数据库用户相同的数据库用户来尝试.
Try it with the same DB-user as the one you have created the table.