MCPcopy Index your code
hub / github.com/sqlc-dev/sqlc / startPostgreSQL

Function startPostgreSQL

cmd/sqlc-test-setup/main.go:404–504  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

402}
403
404func startPostgreSQL() error {
405 log.Println("--- Starting PostgreSQL ---")
406
407 dataDir := pgDataDir()
408 logFile := filepath.Join(pgBaseDir(), "postgresql.log")
409
410 // Check if already running
411 if pgIsReady() {
412 log.Println("postgresql is already running and accepting connections")
413 return nil
414 }
415
416 // Initialize data directory if needed
417 if _, err := os.Stat(filepath.Join(dataDir, "PG_VERSION")); os.IsNotExist(err) {
418 log.Println("initializing postgresql data directory")
419 if err := os.MkdirAll(dataDir, 0o700); err != nil {
420 return fmt.Errorf("creating data directory: %w", err)
421 }
422 if err := run(pgBin("initdb"),
423 "-D", dataDir,
424 "--username=postgres",
425 "--auth=trust",
426 ); err != nil {
427 return fmt.Errorf("initdb: %w", err)
428 }
429
430 // Configure pg_hba.conf for md5 password authentication on TCP
431 hbaPath := filepath.Join(dataDir, "pg_hba.conf")
432 if err := configurePgHBA(hbaPath); err != nil {
433 return fmt.Errorf("configuring pg_hba.conf: %w", err)
434 }
435
436 // Configure postgresql.conf to listen on localhost
437 confPath := filepath.Join(dataDir, "postgresql.conf")
438 if err := appendToFile(confPath,
439 "\n# sqlc-test-setup configuration\n"+
440 "listen_addresses = '127.0.0.1'\n"+
441 "port = 5432\n",
442 ); err != nil {
443 return fmt.Errorf("configuring postgresql.conf: %w", err)
444 }
445 } else {
446 log.Println("postgresql data directory already initialized")
447 }
448
449 // Start PostgreSQL using pg_ctl
450 log.Println("starting postgresql")
451 if err := run(pgBin("pg_ctl"),
452 "-D", dataDir,
453 "-l", logFile,
454 "-o", fmt.Sprintf("-k %s", dataDir),
455 "start",
456 ); err != nil {
457 return fmt.Errorf("pg_ctl start: %w", err)
458 }
459
460 // Wait for PostgreSQL to be ready
461 log.Println("waiting for postgresql to accept connections")

Callers 1

runStartFunction · 0.85

Calls 10

pgDataDirFunction · 0.85
pgBaseDirFunction · 0.85
pgIsReadyFunction · 0.85
pgBinFunction · 0.85
configurePgHBAFunction · 0.85
appendToFileFunction · 0.85
waitForPostgreSQLFunction · 0.85
configurePgHBAWithMD5Function · 0.85
runFunction · 0.70
JoinMethod · 0.45

Tested by

no test coverage detected