(ctx context.Context)
| 42 | } |
| 43 | |
| 44 | func startPostgreSQLServer(ctx context.Context) (string, error) { |
| 45 | // Standard URI for test PostgreSQL |
| 46 | uri := "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable" |
| 47 | |
| 48 | // Try to connect first - it might already be running |
| 49 | if err := waitForPostgres(ctx, uri, 500*time.Millisecond); err == nil { |
| 50 | slog.Info("native/postgres", "status", "already running") |
| 51 | return uri, nil |
| 52 | } |
| 53 | |
| 54 | // Check if PostgreSQL is installed |
| 55 | if _, err := exec.LookPath("psql"); err != nil { |
| 56 | return "", fmt.Errorf("PostgreSQL is not installed (psql not found)") |
| 57 | } |
| 58 | |
| 59 | // Start PostgreSQL service |
| 60 | slog.Info("native/postgres", "status", "starting service") |
| 61 | |
| 62 | // Try systemctl first, fall back to pg_ctlcluster |
| 63 | if err := startPostgresService(); err != nil { |
| 64 | return "", fmt.Errorf("failed to start PostgreSQL: %w", err) |
| 65 | } |
| 66 | |
| 67 | // Configure PostgreSQL for password authentication |
| 68 | if err := configurePostgres(); err != nil { |
| 69 | return "", fmt.Errorf("failed to configure PostgreSQL: %w", err) |
| 70 | } |
| 71 | |
| 72 | // Wait for PostgreSQL to be ready |
| 73 | waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second) |
| 74 | defer cancel() |
| 75 | |
| 76 | if err := waitForPostgres(waitCtx, uri, 30*time.Second); err != nil { |
| 77 | return "", fmt.Errorf("timeout waiting for PostgreSQL: %w", err) |
| 78 | } |
| 79 | |
| 80 | return uri, nil |
| 81 | } |
| 82 | |
| 83 | func startPostgresService() error { |
| 84 | // Try systemctl first |
no test coverage detected