Skip to content

Commit cf8c944

Browse files
committed
add embedded pg
1 parent 3a79e3a commit cf8c944

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

scripts/embedded-pg/main.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Start an embedded postgres database on port 5432. Used in CI on macOS and Windows.
2+
package main
3+
4+
import (
5+
"database/sql"
6+
"os"
7+
"path/filepath"
8+
9+
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
10+
)
11+
12+
func main() {
13+
postgresPath := filepath.Join(os.TempDir(), "coder-test-postgres")
14+
ep := embeddedpostgres.NewDatabase(
15+
embeddedpostgres.DefaultConfig().
16+
Version(embeddedpostgres.V16).
17+
BinariesPath(filepath.Join(postgresPath, "bin")).
18+
DataPath(filepath.Join(postgresPath, "data")).
19+
RuntimePath(filepath.Join(postgresPath, "runtime")).
20+
CachePath(filepath.Join(postgresPath, "cache")).
21+
Username("postgres").
22+
Password("postgres").
23+
Database("postgres").
24+
Encoding("UTF8").
25+
Port(uint32(5432)).
26+
Logger(os.Stdout),
27+
)
28+
err := ep.Start()
29+
if err != nil {
30+
panic(err)
31+
}
32+
// We execute these queries instead of using the embeddedpostgres
33+
// StartParams because it doesn't work on Windows. The library
34+
// seems to have a bug where it sends malformed parameters to
35+
// pg_ctl. It encloses each parameter in single quotes, which
36+
// Windows can't handle.
37+
paramQueries := []string{
38+
`ALTER SYSTEM SET effective_cache_size = '1GB';`,
39+
`ALTER SYSTEM SET fsync = 'off';`,
40+
`ALTER SYSTEM SET full_page_writes = 'off';`,
41+
`ALTER SYSTEM SET max_connections = '1000';`,
42+
`ALTER SYSTEM SET shared_buffers = '1GB';`,
43+
`ALTER SYSTEM SET synchronous_commit = 'off';`,
44+
`ALTER SYSTEM SET client_encoding = 'UTF8';`,
45+
}
46+
db, err := sql.Open("postgres", "postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable")
47+
if err != nil {
48+
panic(err)
49+
}
50+
for _, query := range paramQueries {
51+
if _, err := db.Exec(query); err != nil {
52+
panic(err)
53+
}
54+
}
55+
if err := db.Close(); err != nil {
56+
panic(err)
57+
}
58+
// We restart the database to apply all the parameters.
59+
if err := ep.Stop(); err != nil {
60+
panic(err)
61+
}
62+
if err := ep.Start(); err != nil {
63+
panic(err)
64+
}
65+
}

0 commit comments

Comments
 (0)