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:
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:
pgpm prompts for module details:
Select extensions your module needs. Use arrow keys to navigate and space to select. Common choices:
uuid-ossp- UUID generation functionsplpgsql- PL/pgSQL procedural languagepgcrypto- Cryptographic functions
You can also add the --extensions flag to pre-select:
pgpm scaffolds the module structure:
This structure follows Postgres extension conventions with Constructive's migration tracking.
Step 3: Understanding Module Structure
pets.control - Module metadata:
The .control file declares:
- Module name (from filename)
- Description
- Version
- Dependencies (Postgres extensions or other database modules)
pgpm.plan - Migration plan:
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:
pgpm creates three files:
deploy/schemas/pets.sqlrevert/schemas/pets.sqlverify/schemas/pets.sql
The plan file updates:
Step 5: Define Your Schema
Edit deploy/schemas/pets.sql:
Edit revert/schemas/pets.sql:
Edit verify/schemas/pets.sql:
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:
This creates a nested directory structure:
The plan file shows the dependency:
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:
Edit revert/schemas/pets/tables/pets.sql:
Edit verify/schemas/pets/tables/pets.sql:
Step 7: Deploy Your Module
Create a database for your module:
Deploy your module:
pgpm:
- Resolves dependencies (uuid-ossp, plpgsql)
- Deploys changes in order (schemas/pets, then schemas/pets/tables/pets)
- Tracks deployment in
pgpm_migrateschema - Confirms success
The --yes flag skips confirmation prompts. Remove it to see what will deploy before confirming.
Step 8: Verify Your Deployment
Verify the deployment succeeded:
Or query the table directly:
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- 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.
