Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8b137fc

Browse files
Custom Config Values (fergusstrange#117)
* Fix fergusstrange#94: Support custom start parameters. Add a StartParameters config taking configuration settings. These run-time configuration parameters override those set in the default postgres.conf, and are passed to the postgres process via the options flag of pg_ctl. * Ensure we do run tests for non apple silicon. --------- Co-authored-by: Josh Giles <[email protected]>
1 parent c0fbd81 commit 8b137fc

File tree

5 files changed

+59
-3
lines changed

5 files changed

+59
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ RuntimePath("/tmp").
8787
BinaryRepositoryURL("https://repo.local/central.proxy").
8888
Port(9876).
8989
StartTimeout(45 * time.Second).
90+
StartParameters(map[string]string{"max_connections": "200"}).
9091
Logger(logger))
9192
err := postgres.Start()
9293

config.go

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type Config struct {
1818
dataPath string
1919
binariesPath string
2020
locale string
21+
startParameters map[string]string
2122
binaryRepositoryURL string
2223
startTimeout time.Duration
2324
logger io.Writer
@@ -101,6 +102,15 @@ func (c Config) Locale(locale string) Config {
101102
return c
102103
}
103104

105+
// StartParameters sets run-time parameters when starting Postgres (passed to Postgres via "-c").
106+
//
107+
// These parameters can be used to override the default configuration values in postgres.conf such
108+
// as max_connections=100. See https://www.postgresql.org/docs/current/runtime-config.html
109+
func (c Config) StartParameters(parameters map[string]string) Config {
110+
c.startParameters = parameters
111+
return c
112+
}
113+
104114
// StartTimeout sets the max timeout that will be used when starting the Postgres process and creating the initial database.
105115
func (c Config) StartTimeout(timeout time.Duration) Config {
106116
c.startTimeout = timeout

embedded_postgres.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,20 @@ func (ep *EmbeddedPostgres) Stop() error {
193193
return nil
194194
}
195195

196+
func encodeOptions(port uint32, parameters map[string]string) string {
197+
options := []string{fmt.Sprintf("-p %d", port)}
198+
for k, v := range parameters {
199+
// Single-quote parameter values - they may have spaces.
200+
options = append(options, fmt.Sprintf("-c %s='%s'", k, v))
201+
}
202+
return strings.Join(options, " ")
203+
}
204+
196205
func startPostgres(ep *EmbeddedPostgres) error {
197206
postgresBinary := filepath.Join(ep.config.binariesPath, "bin/pg_ctl")
198207
postgresProcess := exec.Command(postgresBinary, "start", "-w",
199208
"-D", ep.config.dataPath,
200-
"-o", fmt.Sprintf(`"-p %d"`, ep.config.port))
209+
"-o", encodeOptions(ep.config.port, ep.config.startParameters))
201210
postgresProcess.Stdout = ep.syncedLogger.file
202211
postgresProcess.Stderr = ep.syncedLogger.file
203212

embedded_postgres_test.go

+35-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func Test_ErrorWhenCannotStartPostgresProcess(t *testing.T) {
233233

234234
err = database.Start()
235235

236-
assert.EqualError(t, err, fmt.Sprintf("could not start postgres using %s/bin/pg_ctl start -w -D %s/data -o \"-p 5432\":\nah it did not work", extractPath, extractPath))
236+
assert.EqualError(t, err, fmt.Sprintf("could not start postgres using %s/bin/pg_ctl start -w -D %s/data -o -p 5432:\nah it did not work", extractPath, extractPath))
237237
}
238238

239239
func Test_CustomConfig(t *testing.T) {
@@ -416,6 +416,40 @@ func Test_ConcurrentStart(t *testing.T) {
416416
wg.Wait()
417417
}
418418

419+
func Test_CustomStartParameters(t *testing.T) {
420+
database := NewDatabase(DefaultConfig().StartParameters(map[string]string{
421+
"max_connections": "101",
422+
"shared_buffers": "16 MB", // Ensure a parameter with spaces encodes correctly.
423+
}))
424+
if err := database.Start(); err != nil {
425+
shutdownDBAndFail(t, err, database)
426+
}
427+
428+
db, err := sql.Open("postgres", "host=localhost port=5432 user=postgres password=postgres dbname=postgres sslmode=disable")
429+
if err != nil {
430+
shutdownDBAndFail(t, err, database)
431+
}
432+
433+
if err := db.Ping(); err != nil {
434+
shutdownDBAndFail(t, err, database)
435+
}
436+
437+
row := db.QueryRow("SHOW max_connections")
438+
var res string
439+
if err := row.Scan(&res); err != nil {
440+
shutdownDBAndFail(t, err, database)
441+
}
442+
assert.Equal(t, "101", res)
443+
444+
if err := db.Close(); err != nil {
445+
shutdownDBAndFail(t, err, database)
446+
}
447+
448+
if err := database.Stop(); err != nil {
449+
shutdownDBAndFail(t, err, database)
450+
}
451+
}
452+
419453
func Test_CanStartAndStopTwice(t *testing.T) {
420454
database := NewDatabase()
421455

platform-test/platform_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ func Test_AllMajorVersions(t *testing.T) {
1818
embeddedpostgres.V14,
1919
}
2020

21-
if runtime.GOOS != "darwin" && runtime.GOARCH == "arm64" {
21+
isLikelyAppleSilicon := runtime.GOOS == "darwin" && runtime.GOARCH == "arm64"
22+
23+
if !isLikelyAppleSilicon {
2224
allVersions = append(allVersions,
2325
embeddedpostgres.V13,
2426
embeddedpostgres.V12,

0 commit comments

Comments
 (0)