Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (71 loc) · 2.14 KB
/
Copy pathmain.go
File metadata and controls
76 lines (71 loc) · 2.14 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
74
75
76
// Start an embedded postgres database on port 5432. Used in CI on macOS and Windows.
package main
import (
"database/sql"
"flag"
"os"
"path/filepath"
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
)
func main() {
var customPath string
flag.StringVar(&customPath, "path", "", "Optional custom path for postgres data directory")
flag.Parse()
postgresPath := filepath.Join(os.TempDir(), "coder-test-postgres")
if customPath != "" {
postgresPath = customPath
}
ep := embeddedpostgres.NewDatabase(
embeddedpostgres.DefaultConfig().
Version(embeddedpostgres.V16).
BinariesPath(filepath.Join(postgresPath, "bin")).
DataPath(filepath.Join(postgresPath, "data")).
RuntimePath(filepath.Join(postgresPath, "runtime")).
CachePath(filepath.Join(postgresPath, "cache")).
Username("postgres").
Password("postgres").
Database("postgres").
Encoding("UTF8").
Port(uint32(5432)).
Logger(os.Stdout),
)
err := ep.Start()
if err != nil {
panic(err)
}
// We execute these queries instead of using the embeddedpostgres
// StartParams because it doesn't work on Windows. The library
// seems to have a bug where it sends malformed parameters to
// pg_ctl. It encloses each parameter in single quotes, which
// Windows can't handle.
// Related issue:
// https://github.com/fergusstrange/embedded-postgres/issues/145
paramQueries := []string{
`ALTER SYSTEM SET effective_cache_size = '1GB';`,
`ALTER SYSTEM SET fsync = 'off';`,
`ALTER SYSTEM SET full_page_writes = 'off';`,
`ALTER SYSTEM SET max_connections = '1000';`,
`ALTER SYSTEM SET shared_buffers = '1GB';`,
`ALTER SYSTEM SET synchronous_commit = 'off';`,
`ALTER SYSTEM SET client_encoding = 'UTF8';`,
}
db, err := sql.Open("postgres", "postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable")
if err != nil {
panic(err)
}
for _, query := range paramQueries {
if _, err := db.Exec(query); err != nil {
panic(err)
}
}
if err := db.Close(); err != nil {
panic(err)
}
// We restart the database to apply all the parameters.
if err := ep.Stop(); err != nil {
panic(err)
}
if err := ep.Start(); err != nil {
panic(err)
}
}