首页 > 解决方案 > 如何在 psql 客户端中创建表?

问题描述

我有一个可以使用 postico 访问的现有数据库,并且我想将结构迁移到一个只能使用 psql 访问的新数据库。

Postico 给出了重新创建表的 SQL

-- DDL generated by Postico 1.5.8
-- Not all database features are supported. Do not use for backup.

-- Table Definition ----------------------------------------------

CREATE TABLE "Datas" (
    id integer DEFAULT nextval('"Data_id_seq"'::regclass) PRIMARY KEY,
    DataLabel character varying(255) NOT NULL,
    DataValue character varying(255),
    "createdAt" timestamp with time zone NOT NULL,
    "updatedAt" timestamp with time zone NOT NULL
);

-- Indices -------------------------------------------------------

CREATE UNIQUE INDEX "Data_pkey" ON "Datas"(id int4_ops);
CREATE INDEX datas_id ON "Datas"(id int4_ops);

但是,如果我尝试在 psql 客户端中运行创建表,则会收到错误消息

relation "Datas_id_seq" does not exist

如何迁移表结构?我需要手动创建关系吗?如果是这样怎么办?

标签: postgresqlddl

解决方案


您需要先创建序列"Data_id_seq"

create sequence "Data_id_seq";

但该列可能serial最初定义为:

CREATE TABLE "Datas" (
    id serial PRIMARY KEY,
    DataLabel character varying(255) NOT NULL,
    DataValue character varying(255),
    "createdAt" timestamp with time zone NOT NULL,
    "updatedAt" timestamp with time zone NOT NULL
);

推荐阅读