|
1 | 1 | # waitfor-postgres |
2 | | -Postgres resource readiness assertion library |
3 | 2 |
|
4 | | -# Quick start |
| 3 | +[](https://github.com/go-waitfor/waitfor-postgres/actions/workflows/build.yml) |
| 4 | +[](https://golang.org/) |
| 5 | +[](https://opensource.org/licenses/Apache-2.0) |
| 6 | + |
| 7 | +A PostgreSQL resource readiness assertion library built on top of the [waitfor](https://github.com/go-waitfor/waitfor) framework. This library allows you to wait for PostgreSQL databases to become available before proceeding with your application startup, making it ideal for containerized environments, integration tests, and service orchestration. |
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- **PostgreSQL connectivity testing**: Ping PostgreSQL databases to verify they're ready |
| 12 | +- **Multiple URL schemes**: Supports both `postgres://` and `postgresql://` connection strings |
| 13 | +- **Configurable retry logic**: Customize attempts, intervals, and timeouts |
| 14 | +- **Context support**: Full context cancellation and timeout support |
| 15 | +- **Integration ready**: Built for Docker Compose, Kubernetes, and CI/CD pipelines |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```bash |
| 20 | +go get github.com/go-waitfor/waitfor-postgres |
| 21 | +``` |
| 22 | + |
| 23 | +## Quick Start |
5 | 24 |
|
6 | 25 | ```go |
7 | 26 | package main |
8 | 27 |
|
9 | 28 | import ( |
10 | 29 | "context" |
11 | 30 | "fmt" |
| 31 | + "time" |
| 32 | + |
12 | 33 | "github.com/go-waitfor/waitfor" |
13 | 34 | "github.com/go-waitfor/waitfor-postgres" |
14 | | - "os" |
15 | 35 | ) |
16 | 36 |
|
17 | 37 | func main() { |
18 | 38 | runner := waitfor.New(postgres.Use()) |
19 | 39 |
|
| 40 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 41 | + defer cancel() |
| 42 | + |
20 | 43 | err := runner.Test( |
21 | | - context.Background(), |
22 | | - []string{"postgres://localhost:8080/my-db"}, |
23 | | - waitfor.WithAttempts(5), |
| 44 | + ctx, |
| 45 | + []string{"postgres://user:password@localhost/mydb"}, |
| 46 | + waitfor.WithAttempts(10), |
| 47 | + waitfor.WithInterval(2000), // 2000 milliseconds (2 seconds) |
24 | 48 | ) |
25 | 49 |
|
26 | 50 | if err != nil { |
27 | | - fmt.Println(err) |
28 | | - os.Exit(1) |
| 51 | + fmt.Printf("PostgreSQL not ready: %v\n", err) |
| 52 | + return |
29 | 53 | } |
| 54 | + |
| 55 | + fmt.Println("PostgreSQL is ready!") |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +## Usage Examples |
| 60 | + |
| 61 | +### Basic Connection Test |
| 62 | + |
| 63 | +```go |
| 64 | +runner := waitfor.New(postgres.Use()) |
| 65 | + |
| 66 | +err := runner.Test( |
| 67 | + context.Background(), |
| 68 | + []string{"postgres://localhost/mydb"}, |
| 69 | +) |
| 70 | +``` |
| 71 | + |
| 72 | +### Multiple Databases |
| 73 | + |
| 74 | +```go |
| 75 | +databases := []string{ |
| 76 | + "postgres://user:pass@db1:5432/app_db", |
| 77 | + "postgresql://user:pass@db2:5432/cache_db", |
| 78 | +} |
| 79 | + |
| 80 | +err := runner.Test(context.Background(), databases) |
| 81 | +``` |
| 82 | + |
| 83 | +### Custom Configuration |
| 84 | + |
| 85 | +```go |
| 86 | +ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) |
| 87 | +defer cancel() |
| 88 | + |
| 89 | +err := runner.Test( |
| 90 | + ctx, |
| 91 | + []string{"postgres://user:pass@localhost/mydb"}, |
| 92 | + waitfor.WithAttempts(20), // Try up to 20 times |
| 93 | + waitfor.WithInterval(5000), // Wait 5000 milliseconds (5 seconds) between attempts |
| 94 | +) |
| 95 | +``` |
| 96 | + |
| 97 | +### Error Handling |
| 98 | + |
| 99 | +```go |
| 100 | +err := runner.Test(ctx, []string{"postgres://localhost/mydb"}) |
| 101 | +if err != nil { |
| 102 | + // Handle different types of errors |
| 103 | + fmt.Printf("Database connection failed: %v\n", err) |
| 104 | + |
| 105 | + // Check if it's a timeout |
| 106 | + if ctx.Err() == context.DeadlineExceeded { |
| 107 | + fmt.Println("Timed out waiting for database") |
| 108 | + } |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Supported URL Formats |
| 113 | + |
| 114 | +The library supports both PostgreSQL URL schemes: |
| 115 | + |
| 116 | +- `postgres://user:password@host:port/dbname?sslmode=disable` |
| 117 | +- `postgresql://user:password@host:port/dbname?sslmode=require` |
| 118 | + |
| 119 | +### URL Components |
| 120 | + |
| 121 | +``` |
| 122 | +postgresql://username:password@hostname:port/database?param1=value1¶m2=value2 |
| 123 | +``` |
| 124 | + |
| 125 | +- **username**: Database username (optional) |
| 126 | +- **password**: Database password (optional) |
| 127 | +- **hostname**: Database server hostname or IP |
| 128 | +- **port**: Database server port (default: 5432) |
| 129 | +- **database**: Database name (optional) |
| 130 | +- **parameters**: Connection parameters like `sslmode`, `connect_timeout`, etc. |
| 131 | + |
| 132 | +## Configuration Options |
| 133 | + |
| 134 | +The library uses the waitfor framework's configuration options: |
| 135 | + |
| 136 | +| Option | Description | Default | |
| 137 | +|--------|-------------|---------| |
| 138 | +| `WithAttempts(n)` | Maximum number of connection attempts | 30 | |
| 139 | +| `WithInterval(ms)` | Time between connection attempts in milliseconds | 1000 (1 second) | |
| 140 | + |
| 141 | +## Context and Timeouts |
| 142 | + |
| 143 | +Always use context with timeouts for production applications: |
| 144 | + |
| 145 | +```go |
| 146 | +// Set overall timeout for the entire wait operation |
| 147 | +ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) |
| 148 | +defer cancel() |
| 149 | + |
| 150 | +// The Test method will respect the context deadline |
| 151 | +err := runner.Test(ctx, []string{"postgres://localhost/mydb"}, waitfor.WithAttempts(20)) |
| 152 | + |
| 153 | +if errors.Is(err, context.DeadlineExceeded) { |
| 154 | + log.Fatal("Timed out waiting for PostgreSQL to become ready") |
30 | 155 | } |
31 | | -``` |
| 156 | +``` |
| 157 | + |
| 158 | +## Integration Examples |
| 159 | + |
| 160 | +### Docker Compose |
| 161 | + |
| 162 | +```yaml |
| 163 | +version: '3.8' |
| 164 | +services: |
| 165 | + postgres: |
| 166 | + image: postgres:15 |
| 167 | + environment: |
| 168 | + POSTGRES_DB: myapp |
| 169 | + POSTGRES_USER: user |
| 170 | + POSTGRES_PASSWORD: password |
| 171 | + |
| 172 | + app: |
| 173 | + build: . |
| 174 | + depends_on: |
| 175 | + - postgres |
| 176 | + environment: |
| 177 | + DATABASE_URL: postgres://user:password@postgres:5432/myapp |
| 178 | +``` |
| 179 | +
|
| 180 | +### Kubernetes Init Container |
| 181 | +
|
| 182 | +```yaml |
| 183 | +initContainers: |
| 184 | +- name: wait-for-db |
| 185 | + image: my-app:latest |
| 186 | + command: ["/wait-for-postgres"] |
| 187 | + env: |
| 188 | + - name: DATABASE_URL |
| 189 | + value: "postgres://user:pass@postgres-service:5432/mydb" |
| 190 | +``` |
| 191 | +
|
| 192 | +## Troubleshooting |
| 193 | +
|
| 194 | +### Common Connection Issues |
| 195 | +
|
| 196 | +1. **Connection refused**: Check if PostgreSQL is running and accessible |
| 197 | +2. **Authentication failed**: Verify username and password |
| 198 | +3. **Database not found**: Ensure the database exists |
| 199 | +4. **SSL/TLS issues**: Check `sslmode` parameter in connection string |
| 200 | + |
| 201 | +### Debugging Tips |
| 202 | + |
| 203 | +```go |
| 204 | +// Enable detailed logging (if using a logger) |
| 205 | +log.Printf("Testing connection to: %s", databaseURL) |
| 206 | +
|
| 207 | +err := runner.Test(ctx, []string{databaseURL}) |
| 208 | +if err != nil { |
| 209 | + log.Printf("Connection failed: %v", err) |
| 210 | +} |
| 211 | +``` |
| 212 | + |
| 213 | +## License |
| 214 | + |
| 215 | +This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details. |
0 commit comments