How to get it to work with multiple schemas?
I'm using an external database that has several schemas and each script deletes one or more tables in the previous script.
Schema 1 - Public
--
-- pgschema database dump
--
-- Dumped from database version PostgreSQL 17.7
-- Dumped by pgschema version 1.7.1
CREATE TABLE IF NOT EXISTS geotab_database (
name citext NOT NULL,
active boolean DEFAULT true NOT NULL,
id uuid DEFAULT uuid_generate_v1mc(),
api_password text,
CONSTRAINT geotab_database_pk PRIMARY KEY (id)
);
CREATE INDEX IF NOT EXISTS geotab_database_active_idx ON geotab_database (active);
CREATE INDEX IF NOT EXISTS geotab_database_name_idx ON geotab_database (name);
CREATE INDEX IF NOT EXISTS geotab_database_name_idx1 ON geotab_database USING gin (lower(name::text) gin_trgm_ops);
CREATE TABLE IF NOT EXISTS service_state (
id uuid DEFAULT uuid_generate_v1mc(),
service_name citext NOT NULL,
last_run timestamptz,
start_time time,
interval text NOT NULL,
enabled boolean NOT NULL,
last_status text,
CONSTRAINT service_state_pkey PRIMARY KEY (id)
);
Schema 2 - vehicle
--
-- pgschema database dump
--
-- Dumped from database version PostgreSQL 17.7
-- Dumped by pgschema version 1.7.1
CREATE TABLE IF NOT EXISTS vehicle_config (
geotab_database_name public.citext NOT NULL,
vehicle_group_id text,
report_last_sent timestamptz,
enabled boolean DEFAULT false NOT NULL,
vehicle_group_name public.citext,
password text NOT NULL,
id uuid DEFAULT public.uuid_generate_v1mc(),
CONSTRAINT vehicle_config_pk PRIMARY KEY (id)
);
CREATE INDEX IF NOT EXISTS vehicle_config_enabled_idx ON vehicle_config (enabled);
CREATE INDEX IF NOT EXISTS vehicle_config_geotab_database_name_idx ON vehicle_config (geotab_database_name);
CREATE INDEX IF NOT EXISTS vehicle_config_report_last_sent_idx ON vehicle_config (report_last_sent);
CREATE INDEX IF NOT EXISTS vehicle_config_vehicle_group_name_idx ON vehicle_config (vehicle_group_name);
I do the dump from the external database and get the SQL files. I thought the tables from other schemas had the schema prefix on the table. Notice that the vehicle schema just says "vehicle_config" and not "vehicle.vehicle_config".
I dump 3 non-public schemas and none have and schema references anymore.
I run Apply on the public.sql file and both tables get created. Sweet. I run Apply on the vehicle.sql file and it deleted the table created in public.sql and created vehicle_config in the public schema instead of the vehicle schema.
Example dump command. dump --host host.docker.internal --db support --user postgres --port 5432 --no-comments --schema public > s_public.sql
Example apply command. apply --host host.docker.internal --db support --user postgres --port 5433 --plan-host host.docker.internal --plan-db support --plan-user postgres --plan-port 5432 --file s_public.sql --auto-approve
I've run into issues with schema objects that have the same name in different schemas. It seems they get copied to the temp schema with the same name and then one shadows the other. I can work around it by adding more explicit schema references than otherwise necessary, so haven't bothered reporting it yet.
Thanks for reporting this. After investigation, this is working as designed.
How pgschema handles multiple schemas
pgschema operates on one schema at a time and produces schema-agnostic dumps — the output contains unqualified DDL (e.g., CREATE TABLE vehicle_config, not vehicle.vehicle_config). This is intentional: the same dump file can be applied to any target schema.
Because of this, the --schema flag must be specified on all three commands when working with non-public schemas:
# Dump each schema
pgschema dump --schema public > s_public.sql
pgschema dump --schema vehicle > s_vehicle.sql
# Apply each schema — --schema is required for non-public schemas
pgschema apply --schema public --file s_public.sql --host ... --plan-host ...
pgschema apply --schema vehicle --file s_vehicle.sql --host ... --plan-host ...
What happened
Your apply command for the vehicle dump was missing --schema vehicle:
# This defaults to --schema public, which compares vehicle's tables against the public schema
apply --file s_vehicle.sql --host ... --plan-host ...
# This is what you need
apply --schema vehicle --file s_vehicle.sql --host ... --plan-host ...
Without --schema vehicle, pgschema defaults to public, inspects the public schema, sees that geotab_database and service_state exist but aren't in the vehicle dump, and generates a plan to drop them and create vehicle_config in public instead.
The cross-schema type references are correct
You noticed public.citext and public.uuid_generate_v1mc() in the vehicle dump — this is correct behavior. pgschema schema-qualifies references to objects in other schemas while leaving objects in the target schema unqualified.