A practical PostgreSQL laboratory focused on writing production-style database code using PL/pgSQL, JSONB, triggers, business rules, and advanced PostgreSQL features.
This project follows the development of a realistic booking platform, implementing business logic directly inside PostgreSQL through practical development scenarios.
- Demonstrate advanced PostgreSQL development techniques.
- Implement business logic directly inside PostgreSQL.
- Explore production-oriented PL/pgSQL patterns.
- Showcase practical use of JSONB, triggers, and advanced database features.
- Serve as a reference for PostgreSQL application development.
- The project is intentionally organized as progressive scenarios, where each topic builds upon previous concepts.
- Git
- PowerShell 7+
- PostgreSQL 17 (or compatible)
- pgAdmin,
psql, or another PostgreSQL client
Start a PostgreSQL instance using the provided Docker Compose file:
docker compose up -dThe container will be created with the following default configuration:
- Database:
postgresql_development_lab - Username:
postgres - Password:
postgres - Port:
5432
Run the build script to generate the consolidated installation file:
./tools/build-all.ps1This generates:
install/
└── postgresql-development-lab.sql
Open the generated script in pgAdmin (or your preferred PostgreSQL client) and execute:
install/postgresql-development-lab.sql
To preserve existing data, do not run the destructive consolidated installer. Apply the upgrades in order instead:
schema/upgrades/001_add_property_type.sql
schema/upgrades/002_add_property_code.sql
schema/upgrades/003_update_property_creation.sql
schema/upgrades/004_add_guest_is_active.sql
schema/upgrades/005_add_guest_documents.sql
schema/upgrades/006_add_application_users.sql
schema/upgrades/007_increase_monetary_precision.sql
Because historical rows have no reliable type information, the upgrade assigns
ROOM as a transitional value. Review and correct those rows after applying it.
The guest upgrade keeps every existing guest active. Guests can then be soft
deleted with UPDATE guests SET is_active = FALSE; it does not remove rows or
their reservation history. Existing queries are not filtered by activity.
After the installation completes, execute the scripts located in the examples directory to see the implemented functions and procedures in action.
examples/
├── 01_plpgsql_fundamentals.sql
├── 02_functions_business_rules.sql
├── 03_triggers_and_auditing.sql
├── 04_error_handling_and_logging.sql
├── 05_advanced_types.sql
├── 06_jsonb.sql
├── 07_property_codes.sql
├── 08_guest_soft_delete.sql
├── 09_guest_documents.sql
└── 10_application_users.sql
Guest soft-delete behavior and reservation-history preservation are verified by
examples/08_guest_soft_delete.sql.
Guest document normalization, consistency, and uniqueness are verified by
examples/09_guest_documents.sql.
Application users and reservation creator ownership are verified by
examples/10_application_users.sql. Every reservation must reference the
application user that created it; guest_id continues to identify the person
staying or holding the reservation.
The initial development user is CALVAREZ. Its seeded password_hash is an
explicit non-credential placeholder. Booking API/Auth is responsible for
generating and managing real password hashes; PostgreSQL only stores them.
The example scripts simulate a typical booking lifecycle, including:
- booking validation;
- availability checking;
- stay cost calculation;
- discount application;
- reservation management;
- service management;
- payment processing;
- automatic auditing.
- Relational schema design
- Constraints
- Foreign keys
- Custom types
- Seed data
- Functions
- Procedures
- Variables
- Control structures
- Loops
- Exception handling
- Composite types
Examples include:
- Booking validation
- Availability checking
- Stay cost calculation
- Weekly discounts
- Guest booking limits
- Reservation management
Examples of:
- JSONB updates
- Nested JSON manipulation
- JSON arrays
- Metadata storage
- Composite types
- Arrays
- FOREACH loops
- Record variables
- Automatic auditing
- Change history
- Trigger-based business logic
- Custom exceptions
- Validation rules
- Defensive programming
.
├── assets
│ └── erd-1.png
│
├── docs
│ ├── REQ-001-application-users.md
│ └── repository-audit-2026-08-25.md
│
├── examples
│ ├── 01_plpgsql_fundamentals.sql
│ ├── 02_functions_business_rules.sql
│ ├── 03_triggers_and_auditing.sql
│ ├── 04_error_handling_and_logging.sql
│ ├── 05_advanced_types.sql
│ ├── 06_jsonb.sql
│ ├── 07_property_codes.sql
│ ├── 08_guest_soft_delete.sql
│ ├── 09_guest_documents.sql
│ └── 10_application_users.sql
│
├── install
│ ├── 01_plpgsql_fundamentals.sql
│ ├── 02_functions_business_rules.sql
│ ├── 03_triggers_and_auditing.sql
│ ├── 04_error_handling_and_logging.sql
│ ├── 05_advanced_types.sql
│ ├── 06_jsonb.sql
│ └── postgresql-development-lab.sql
│
├── schema
│ ├── 00_custom_types.sql
│ ├── 01_core_schema.sql
│ ├── 02_seed_data.sql
│ └── upgrades
│ ├── 001_add_property_type.sql
│ ├── 002_add_property_code.sql
│ ├── 003_update_property_creation.sql
│ ├── 004_add_guest_is_active.sql
│ ├── 005_add_guest_documents.sql
│ └── 006_add_application_users.sql
│
├── scenarios
│ ├── 01_plpgsql_fundamentals
│ ├── 02_functions_business_rules
│ ├── 03_triggers_and_auditing
│ ├── 04_error_handling_and_logging
│ ├── 05_advanced_types
│ └── 06_jsonb
│
├── tools
│ ├── build-all.ps1
│ └── build-scenarios.ps1
│
├── utils
│ ├── get_setting.sql
│ └── get_table_columns.sql
│
├── docker-compose.yml
└── README.md
| Scenario | Status |
|---|---|
| PL/pgSQL Fundamentals | ✅ |
| Business Rules | ✅ |
| Triggers & Auditing | ✅ |
| Error Handling | ✅ |
| Advanced Types | ✅ |
| JSONB | ✅ |
- PostgreSQL
- PL/pgSQL
- JSONB
- Docker
- PowerShell (build scripts)
Additional scenarios may be added over time as the laboratory evolves.
MIT
