Repository navigation
Expand file tree
/
Copy pathpostgres.go
More file actions
73 lines (65 loc) · 2.71 KB
/
Copy pathpostgres.go
File metadata and controls
73 lines (65 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package shared
import (
"bytes"
"log"
"os"
"path/filepath"
)
func maybePostgresProcFile() (string, error) {
// PG is already configured
if os.Getenv("PGHOST") != "" || os.Getenv("PGDATASOURCE") != "" {
return "", nil
}
// Postgres needs to be able to write to run
var output bytes.Buffer
e := execer{Out: &output}
e.Command("mkdir", "-p", "/run/postgresql")
e.Command("chown", "-R", "postgres", "/run/postgresql")
if err := e.Error(); err != nil {
log.Printf("Setting up postgres failed:\n%s", output.String())
return "", err
}
// postgres wants its config in the data dir
path := filepath.Join(os.Getenv("DATA_DIR"), "postgresql")
if _, err := os.Stat(path); err != nil {
if !os.IsNotExist(err) {
return "", err
}
if verbose {
log.Printf("Setting up PostgreSQL at %s", path)
}
log.Println("✱ Sourcegraph is initializing the internal database... (may take 15-20 seconds)")
var output bytes.Buffer
e := execer{Out: &output}
e.Command("mkdir", "-p", path)
e.Command("chown", "postgres", path)
// initdb --nosync saves ~3-15s on macOS during initial startup. By the time actual data lives in the
// DB, the OS should have had time to fsync.
e.Command("su-exec", "postgres", "initdb", "-D", path, "--nosync")
e.Command("su-exec", "postgres", "pg_ctl", "-D", path, "-o -c listen_addresses=127.0.0.1", "-l", "/tmp/pgsql.log", "-w", "start")
e.Command("su-exec", "postgres", "createdb", "sourcegraph")
e.Command("su-exec", "postgres", "pg_ctl", "-D", path, "-m", "fast", "-l", "/tmp/pgsql.log", "-w", "stop")
if err := e.Error(); err != nil {
log.Printf("Setting up postgres failed:\n%s", output.String())
os.RemoveAll(path)
return "", err
}
} else {
// Between restarts the owner of the volume may have changed. Ensure
// postgres can still read it.
var output bytes.Buffer
e := execer{Out: &output}
e.Command("chown", "-R", "postgres", path)
if err := e.Error(); err != nil {
log.Printf("Adjusting fs owners for postgres failed:\n%s", output.String())
return "", err
}
}
// Set PGHOST to default to 127.0.0.1, NOT localhost, as localhost does not correctly resolve in some environments
// (see https://github.com/sourcegraph/issues/issues/34 and https://github.com/sourcegraph/sourcegraph/issues/9129).
SetDefaultEnv("PGHOST", "127.0.0.1")
SetDefaultEnv("PGUSER", "postgres")
SetDefaultEnv("PGDATABASE", "sourcegraph")
SetDefaultEnv("PGSSLMODE", "disable")
return "postgres: su-exec postgres sh -c 'postgres -c listen_addresses=127.0.0.1 -D " + path + "' 2>&1 | grep -v 'database system was shut down' | grep -v 'MultiXact member wraparound' | grep -v 'database system is ready' | grep -v 'autovacuum launcher started' | grep -v 'the database system is starting up'", nil
}