The dump command extracts a PostgreSQL database schema for a specific schema and outputs it in a developer-friendly format. The dumped schema serves as a baseline that developers can modify and apply to target databases using the plan and apply commands.
Output file path (required when —multi-file is used)For single-file mode, this is optional (defaults to stdout).
For multi-file mode, this specifies the main file path.
---- pgschema database dump---- Dumped from database version PostgreSQL 17.5-- Dumped by pgschema version 1.0.0---- Name: users; Type: TABLE; Schema: -; Owner: ---CREATE TABLE IF NOT EXISTS users ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255) UNIQUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);---- Name: idx_users_email; Type: INDEX; Schema: -; Owner: ---CREATE INDEX IF NOT EXISTS idx_users_email ON users(email);
pgschema uses smart schema qualification to make dumps portable:
Objects within the dumped schema: No schema qualifier added
Objects from other schemas: Fully qualified with schema name
This approach makes the dump suitable as a baseline that can be applied to different schemas, particularly useful for multi-tenant applications.
Copy
# Dump the 'public' schemapgschema dump --host localhost --db myapp --user postgres --schema public
Output for objects within ‘public’ schema (no qualification):
Copy
-- Objects in the dumped schema have no qualifierCREATE TABLE users ( id SERIAL PRIMARY KEY, name VARCHAR(255) NOT NULL);CREATE TABLE orders ( id SERIAL PRIMARY KEY, user_id INTEGER REFERENCES users(id), -- Same schema, no qualifier product_id INTEGER REFERENCES catalog.products(id) -- Different schema, qualified);
This qualification strategy enables using one schema as a template for multiple tenants: