Postgres Backend
The Postgres (PostgreSQL) backend stores state in a PostgreSQL database table with automatic schema management and state locking.Implementation
Location:/internal/backend/remote-state/pg/backend.go
Use Cases
- Existing PostgreSQL infrastructure
- Teams already using PostgreSQL
- Strong ACID compliance requirements
- Integration with PostgreSQL-based applications
- On-premises deployments with PostgreSQL
Basic Configuration
Required Configuration
conn_str
- Type: String
- Optional: Yes (if environment variable is set)
- Environment Variable:
PG_CONN_STR - Description: PostgreSQL connection string
postgres://username:password@host:port/database?sslmode=require
Optional Configuration
schema_name
- Type: String
- Optional: Yes
- Default:
"terraform_remote_state" - Environment Variable:
PG_SCHEMA_NAME - Description: Name of the PostgreSQL schema to use
skip_schema_creation is enabled.
skip_schema_creation
- Type: Boolean
- Optional: Yes
- Default:
false - Environment Variable:
PG_SKIP_SCHEMA_CREATION - Description: Skip automatic schema creation
true if the user doesn’t have CREATE SCHEMA privilege.
skip_table_creation
- Type: Boolean
- Optional: Yes
- Default:
false - Environment Variable:
PG_SKIP_TABLE_CREATION - Description: Skip automatic table creation
true if you want to create the table manually.
skip_index_creation
- Type: Boolean
- Optional: Yes
- Default:
false - Environment Variable:
PG_SKIP_INDEX_CREATION - Description: Skip automatic index creation
Database Schema
The backend automatically creates:Schema
Sequence
Table
Index
Connection String Format
The connection string supports various formats:Basic Format
With SSL
SSL Modes
disable- No SSLrequire- Require SSL (default)verify-ca- Verify CA certificateverify-full- Verify CA and hostname
Unix Socket
With Additional Parameters
Workspaces
The Postgres backend supports workspaces. Each workspace is stored as a separate row:| name | data |
|---|---|
default | {state JSON} |
development | {state JSON} |
production | {state JSON} |
State Locking
The Postgres backend uses PostgreSQL’s advisory locks for state locking:- Automatic locking during state-modifying operations
- Database-level locking ensures consistency
- Locks are automatically released on connection close
Configuration Options Summary
| Option | Type | Required | Default | Description |
|---|---|---|---|---|
conn_str | string | Yes* | - | PostgreSQL connection string |
schema_name | string | No | terraform_remote_state | Schema name |
skip_schema_creation | bool | No | false | Skip schema creation |
skip_table_creation | bool | No | false | Skip table creation |
skip_index_creation | bool | No | false | Skip index creation |
PG_CONN_STR environment variable is set.
Example: Development Setup
Example: Production with SSL
Example: Using Environment Variable
Example: Manual Schema Management
If you don’t haveCREATE SCHEMA privileges:
1. DBA creates schema and grants permissions:
Required Database Permissions
Minimum Permissions (Auto-create)
Minimum Permissions (Manual setup)
Database Setup Best Practices
1. Create Dedicated Database
2. Create Dedicated User
3. Enable SSL
4. Connection Pooling
For high-traffic environments, use connection pooling with PgBouncer:Advantages
- ACID Compliance - Strong consistency guarantees
- Native Locking - Advisory locks prevent concurrent modifications
- Familiar Technology - PostgreSQL is widely known and used
- Integrated - Works with existing PostgreSQL infrastructure
- Transactional - State operations are atomic
- Auditing - Database logs track all changes
Limitations
- Database Dependency - Requires PostgreSQL server
- Size Limits - Large states may impact database performance
- No Versioning - No built-in state history (use database backups)
- Network Latency - Remote database connections may be slower
Monitoring and Maintenance
Check State Size
View All Workspaces
Check for Locks
Best Practices
- Use SSL for all connections
- Dedicated database for Terraform state
- Regular backups of the state database
- Connection pooling for multiple users
- Monitor database size and performance
- Separate schemas for different teams
- Rotate credentials regularly
- Enable query logging for audit trails