1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.arvados.org/arvados.git/sdk/go/arvados"
21 func runPostgres(ctx context.Context, boot *Booter, ready chan<- bool) error {
22 buf := bytes.NewBuffer(nil)
23 err := boot.RunProgram(ctx, boot.tempdir, buf, nil, "pg_config", "--bindir")
27 datadir := filepath.Join(boot.tempdir, "pgdata")
29 err = os.Mkdir(datadir, 0755)
33 bindir := strings.TrimSpace(buf.String())
35 err = boot.RunProgram(ctx, boot.tempdir, nil, nil, filepath.Join(bindir, "initdb"), "-D", datadir)
40 err = boot.RunProgram(ctx, boot.tempdir, nil, nil, "cp", "server.crt", "server.key", datadir)
45 port := boot.cluster.PostgreSQL.Connection["port"]
47 ctx, cancel := context.WithCancel(ctx)
55 if exec.CommandContext(ctx, "pg_isready", "--timeout=10", "--host="+boot.cluster.PostgreSQL.Connection["host"], "--port="+port).Run() == nil {
58 time.Sleep(time.Second / 2)
60 db, err := sql.Open("postgres", arvados.PostgreSQLConnection{
66 boot.logger.WithError(err).Error("db open failed")
71 conn, err := db.Conn(ctx)
73 boot.logger.WithError(err).Error("db conn failed")
78 _, err = conn.ExecContext(ctx, `CREATE USER `+pq.QuoteIdentifier(boot.cluster.PostgreSQL.Connection["user"])+` WITH SUPERUSER ENCRYPTED PASSWORD `+pq.QuoteLiteral(boot.cluster.PostgreSQL.Connection["password"]))
80 boot.logger.WithError(err).Error("createuser failed")
84 _, err = conn.ExecContext(ctx, `CREATE DATABASE `+pq.QuoteIdentifier(boot.cluster.PostgreSQL.Connection["dbname"]))
86 boot.logger.WithError(err).Error("createdb failed")
94 return boot.RunProgram(ctx, boot.tempdir, nil, nil, filepath.Join(bindir, "postgres"),
96 "-D", datadir, // data dir
97 "-k", datadir, // socket dir
98 "-p", boot.cluster.PostgreSQL.Connection["port"],