如何将列设置为主键并从终端在 postgres 中自动递增?
How can I set a column to be primary key and autoincremented in postgres from terminal?
我想使用以下语句在 persondb
架构中创建 table persons_table
:
create table persondb.persons_table(
id int,
first_name varchar,
last_name varchar,
age int
);
但是我想把id
设为主键并自增,如何从bash开始?
我正在使用 Postgres,它在 docker 容器中 运行,对于 UI 部分,我正在使用 DBeaver。
使用 shell,您可以这样调用 psql
:
psql -U postgres -d mydatabase <<"EOF"
ALTER TABLE persondb.persons_table
ALTER id PRIMARY KEY GENERATED ALWAYS AS IDENTITY;
EOF
这在 bash 中称为 "here document"。
我想使用以下语句在 persondb
架构中创建 table persons_table
:
create table persondb.persons_table(
id int,
first_name varchar,
last_name varchar,
age int
);
但是我想把id
设为主键并自增,如何从bash开始?
我正在使用 Postgres,它在 docker 容器中 运行,对于 UI 部分,我正在使用 DBeaver。
使用 shell,您可以这样调用 psql
:
psql -U postgres -d mydatabase <<"EOF"
ALTER TABLE persondb.persons_table
ALTER id PRIMARY KEY GENERATED ALWAYS AS IDENTITY;
EOF
这在 bash 中称为 "here document"。