Creating Your First Postgres Database Module
Dan Lynch
Nov 14, 2025
Previously: In Initializing a Workspace for Modular Postgres Development, we set up a pgpm workspace. Now let's create your first database module.
Creating your first database module is where modular database development shines. You'll scaffold a complete module structure with deploy, revert, and verify scripts—all generated automatically. By the end of this lesson, you'll have a working module deployed to Postgres.
Prerequisites
See Prerequisites. Requires: Complete Initializing a Workspace for Modular Postgres Development.
Step 1: Navigate to Your Workspace
Navigate to your pgpm workspace directory:
bashcd my-database-project
pgpm needs to be inside a workspace to create modules. The CLI detects workspaces by finding the pgpm.json file.
Step 2: Create Your First Module
Initialize a new module:
bashpgpm init
pgpm prompts for the module name:
sh? Enter the module name: pets
That's it—pgpm init scaffolds a zero-config module with no extensions. You add extensions explicitly, only when you need them (covered in Managing Dependencies). This keeps modules minimal and scriptable, and matches how tools like Sqitch, Prisma, and Drizzle scaffold.
If you already know which extensions you want, pre-select them up front:
shpgpm init --extensions pgcrypto,citext # non-interactive pgpm init --with-extensions # interactive picker
Or, if you're not running interactively (for example, as an agent):
bashpgpm init --no-tty --moduleName pets --moduleDesc "Pets module"
Author, repo, and license are inherited from the workspace.
Common extensions you might add later:
pgcrypto- cryptographic functionscitext- case-insensitive textpostgis- geospatial types and functions
Note: You rarely need
plpgsql(always present in Postgres) oruuid-ossp(legacy—prefer the built-ingen_random_uuid(), available since Postgres 13).
pgpm scaffolds the module structure:
textpackages/pets/ ├── pets.control ├── pgpm.plan ├── deploy/ ├── revert/ └── verify/
This structure follows Postgres extension conventions with Constructive's migration tracking.
Step 3: Understanding Module Structure
pets.control - Module metadata:
sh# pets.control comment = 'Pet adoption module' default_version = '0.0.1'
The .control file declares:
- Module name (from filename)
- Description
- Version
- Dependencies (Postgres extensions or other database modules), added via a
requiresline once you enable extensions
Since we scaffolded with no extensions, there is no requires line yet. It appears once you add a dependency (see Managing Dependencies).
pgpm.plan - Migration plan:
sh%syntax-version=1.0.0 %project=pets %uri=pets
The plan file tracks all changes in deployment order. As you add changes, they appear here with timestamps and dependencies.
deploy/, revert/, verify/ - Migration directories:
deploy/- SQL to apply changesrevert/- SQL to undo changesverify/- SQL to validate changes
This three-script pattern ensures every change is reversible and verifiable.
Step 4: Add Your First Change
Navigate to your module and add a change:
bashcd packages/pets pgpm add schemas/pets
pgpm creates three files:
deploy/schemas/pets.sqlrevert/schemas/pets.sqlverify/schemas/pets.sql
The plan file updates:
sh%syntax-version=1.0.0 %project=pets %uri=pets schemas/pets 2025-11-14T00:00:00Z Author <author@example.com> # Adds schemas/pets
Step 5: Define Your Schema
Edit deploy/schemas/pets.sql:
sql-- Deploy schemas/pets CREATE SCHEMA pets;
Edit revert/schemas/pets.sql:
sql-- Revert schemas/pets DROP SCHEMA pets CASCADE;
Edit verify/schemas/pets.sql:
sql-- Verify schemas/pets SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'pets';
The verify script confirms the schema exists by querying the information schema.
Step 6: Add a Table Change
Add another change for the pets table that depends on the schema:
bashpgpm add schemas/pets/tables/pets --requires schemas/pets
This creates a nested directory structure:
textdeploy/ └── schemas/ └── pets/ └── tables/ └── pets.sql
The plan file shows the dependency:
shschemas/pets/tables/pets [schemas/pets] 2025-11-14T00:00:00Z Author <author@example.com> # Adds schemas/pets/tables/pets
Nested paths organize changes hierarchically. This is useful for complex modules with many tables, functions, and triggers.
Note: The
--requiresflag is optional. If you usepgpm addchronologically, changes deploy in order like git commits. However, explicitly declaring dependencies helps with advanced features likepgpm plan, which rebuilds deployment plans based on dependencies.
Edit deploy/schemas/pets/tables/pets.sql:
sql-- Deploy schemas/pets/tables/pets -- requires: schemas/pets CREATE TABLE pets.pets ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name TEXT NOT NULL, species TEXT NOT NULL, age INTEGER, adopted BOOLEAN DEFAULT false, created_at TIMESTAMPTZ DEFAULT now() );
Edit revert/schemas/pets/tables/pets.sql:
sql-- Revert schemas/pets/tables/pets DROP TABLE pets.pets;
Edit verify/schemas/pets/tables/pets.sql:
sql-- Verify schemas/pets/tables/pets SELECT table_name FROM information_schema.tables WHERE table_schema = 'pets' AND table_name = 'pets';
Step 7: Deploy Your Module
Create a database for your module:
bashcreatedb pets_dev
Deploy your module:
bashpgpm deploy --database pets_dev
Or, if you're not running interactively (for example, as an agent):
bashpgpm deploy --database pets_dev --package pets --yes
pgpm:
- Resolves dependencies (none for this module yet)
- Deploys changes in order (schemas/pets, then schemas/pets/tables/pets)
- Tracks deployment in
pgpm_migrateschema - Confirms success
Our
petstable uses the built-ingen_random_uuid(), so it needs no extensions at all.
The --yes flag skips confirmation prompts. Remove it to see what will deploy before confirming.
Step 8: Verify Your Deployment
Verify the deployment succeeded:
bashpgpm verify --database pets_dev
Or, if you're not running interactively (for example, as an agent):
bashpgpm verify --database pets_dev --package pets
Or query the table directly:
bashpsql -d pets_dev -c "SELECT * FROM pets.pets;"
You should see an empty table with the columns you defined. Your module is now deployed and working correctly!
What's Next
You've created and deployed your first database module. In the next lesson, we'll add tests to your module with instant feedback using watch mode—enabling rapid test-driven development.
Key Takeaways
pgpm initscaffolds a complete module structure with no extensions by default- Add extensions up front with
--extensions/--with-extensions, or later withpgpm extension - The
.controlfile declares module metadata and dependencies - The
pgpm.planfile tracks changes in deployment order pgpm addcreates deploy, revert, and verify scripts- Nested paths organize changes hierarchically
pgpm deploydeploys modules with dependency resolutionpgpm verifyvalidates deployment success
Your first module is deployed and working. Let's explore multi-module dependencies next.

