Search code examples
postgresqlddl

How can I create a table in psql client?


I have an existing Database which I can access with postico, and I want to migrate the structure to a new database which I can only access with psql.

Postico gives the SQL to re-create the table

-- 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);

But if I try to run the create table in the psql client I get the error

relation "Datas_id_seq" does not exist

How can I migrate the table structure across? Do I need to manually create the relation? if so how?


Solution

  • You need to create the sequence "Data_id_seq" first:

    create sequence "Data_id_seq";
    

    but the column was probably defined as serial originally:

    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
    );