PostgreSQL: export writes enum defaults unquoted and array defaults as a string literal, dump cannot be imported
Steps to reproduce
CREATE TYPE "Status" AS ENUM ('DRAFT', 'PAID'); CREATE TYPE "Kind" AS ENUM ('PRIMARY', 'SECONDARY'); CREATE TYPE "Role" AS ENUM ('USER', 'ADMIN');
CREATE TABLE t ( id serial PRIMARY KEY, status "Status" DEFAULT 'DRAFT' NOT NULL, kind "Kind" DEFAULT 'PRIMARY' NOT NULL, role "Role" DEFAULT 'USER' NOT NULL, tags text[] DEFAULT ARRAY[]::text[] );
Export the table (Output: SQL, Tables: DROP+CREATE), then import the file into an empty database with psql -f.
Actual export
CREATE TABLE "public"."t" ( ... "status" "Status" DEFAULT DRAFT NOT NULL, "kind" "Kind" DEFAULT PRIMARY NOT NULL, "role" "Role" DEFAULT USER NOT NULL, "tags" text[] DEFAULT 'ARRAY[]', ... );
Import errors
Every CREATE TABLE with one of these columns fails, and so do its data, indexes and foreign keys:
ERROR: cannot use column reference in DEFAULT expression LINE 5: "status" "Status" DEFAULT DRAFT NOT NULL,
ERROR: syntax error at or near "PRIMARY" LINE 6: "kind" "Kind" DEFAULT PRIMARY NOT NULL,
ERROR: column "role" is of type "Role" but default expression is of type name
ERROR: malformed array literal: "ARRAY[]" DETAIL: Array value must start with "{" or dimension information.
The third one never mentions quoting: unquoted USER is the SQL function that returns the current role name, so PostgreSQL reports a type mismatch.
Expected export
"status" "Status" DEFAULT 'DRAFT' NOT NULL,
"kind" "Kind" DEFAULT 'PRIMARY' NOT NULL,
"role" "Role" DEFAULT 'USER' NOT NULL,
"tags" text[] DEFAULT ARRAY[]::text[],Keeping the casts as returned by pg_get_expr() ('DRAFT'::"Status", ARRAY[]::text[]) would also work.
Source: vrana/adminer