This is the multi-page printable view of this section. .
Namespace Isolation
Namespaces allow you to create completely isolated environments on a single host, perfect for separating production, development, and testing environments without container name conflicts or port collisions.
What is a Namespace?
A namespace is a prefix applied to all container names within a configuration file. This isolation mechanism ensures that:
- Container names don’t clash: Each namespace gets its own container prefix
- Port ranges are separate: Each config file allocates ports from its own range
- Backup containers are isolated: Each namespace has its own pgBackRest container
- Configuration files are independent: Each environment uses a separate config file
Use Case: Production and Development Environments
A common scenario is running production and development environments on the same server:
This creates two completely isolated environments:
| Environment | Config File | Container Prefix | PG Port Range | SSH Port Range |
|---|---|---|---|---|
| Production | ~/.pgcli-prod/pg.yaml |
pgcli-pg-prod-* |
35432+ | 42201+ |
| Development | ~/.pgcli-dev/pg.yaml |
pgcli-pg-dev-* |
38000+ | 43000+ |
Managing Multiple Environments
Use the -c flag to specify which configuration file to use:
How It Works
Container Naming
With namespace prod and instance app-db:
- Instance container:
pgcli-pg-prod-app-db - Backup container:
pgcli-backup-prod - Network:
pgcli-net-prod(if using separate networks)
Without namespace (or --namespace ""):
- Instance container:
pgcli-pg-default-app-db - Backup container:
pgcli-backup-default
Port Allocation
Each instance in a config file gets sequential ports:
- First instance:
pg_start_port(e.g., 35432) - Second instance:
pg_start_port + 1(e.g., 35433) - And so on…
Same for SSH ports used by pgBackRest.
Configuration Persistence
The namespace and port ranges are saved in the config file:
Best Practices
1. Always Use Explicit Namespaces
Never rely on the default namespace when running multiple configs on one host:
2. Use Disjoint Port Ranges
Ensure port ranges don’t overlap between configs:
Leave headroom for multiple instances within each environment.
3. Namespace is Baked into Container Names
The namespace is embedded in container names at creation time. Changing it later breaks the link:
4. Use Shell Aliases for Convenience
Create aliases to avoid typing -c repeatedly:
Advanced: Multiple Environments with Replicas
You can even set up isolated replication environments:
Each environment maintains its own replication slots, backup stanzas, and data directories.
Troubleshooting
Container Name Conflicts
Cause: Two configs using the same namespace.
Solution: Use different namespaces or destroy the conflicting instance first.
Port Already in Use
Cause: Port ranges overlap between configs.
Solution: Use disjoint port ranges with sufficient spacing.
Instance Not Found After Namespace Change
Cause: Changed namespace in config file after creating instances.
Solution: Either destroy and recreate instances, or revert the namespace change.
Summary
Namespaces provide complete isolation for multiple environments on a single host:
- Separate configs: Each environment gets its own
pg.yaml - Distinct namespaces: Prevents container name collisions
- Disjoint ports: Avoids port conflicts
- Independent operation: Each environment managed separately with
-c
Perfect for running production, development, testing, and staging environments on the same server without interference.