MCPcopy
hub / github.com/sqlc-dev/sqlc / startPostgreSQLServer

Function startPostgreSQLServer

internal/sqltest/native/postgres.go:44–81  ·  view source on GitHub ↗
(ctx context.Context)

Source from the content-addressed store, hash-verified

42}
43
44func 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
83func startPostgresService() error {
84 // Try systemctl first

Callers 1

StartPostgreSQLServerFunction · 0.70

Calls 3

waitForPostgresFunction · 0.85
startPostgresServiceFunction · 0.85
configurePostgresFunction · 0.85

Tested by

no test coverage detected