Skip to content

Fix #94: Support custom start parameters. #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ RuntimePath("/tmp").
BinaryRepositoryURL("https://repo.local/central.proxy").
Port(9876).
StartTimeout(45 * time.Second).
StartParameters(map[string]string{"max_connections": "200"}).
Logger(logger))
err := postgres.Start()

Expand Down
10 changes: 10 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Config struct {
dataPath string
binariesPath string
locale string
startParameters map[string]string
binaryRepositoryURL string
startTimeout time.Duration
logger io.Writer
Expand Down Expand Up @@ -100,6 +101,15 @@ func (c Config) Locale(locale string) Config {
return c
}

// StartParameters sets run-time parameters when starting Postgres (passed to Postgres via "-c").
//
// These parameters can be used to override the default configuration values in postgres.conf such
// as max_connections=100. See https://www.postgresql.org/docs/current/runtime-config.html
func (c Config) StartParameters(parameters map[string]string) Config {
c.startParameters = parameters
return c
}

// StartTimeout sets the max timeout that will be used when starting the Postgres process and creating the initial database.
func (c Config) StartTimeout(timeout time.Duration) Config {
c.startTimeout = timeout
Expand Down
11 changes: 10 additions & 1 deletion embedded_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,20 @@ func (ep *EmbeddedPostgres) Stop() error {
return nil
}

func encodeOptions(port uint32, parameters map[string]string) string {
options := []string{fmt.Sprintf("-p %d", port)}
for k, v := range parameters {
// Single-quote parameter values - they may have spaces.
options = append(options, fmt.Sprintf("-c %s='%s'", k, v))
}
return strings.Join(options, " ")
}

func startPostgres(ep *EmbeddedPostgres) error {
postgresBinary := filepath.Join(ep.config.binariesPath, "bin/pg_ctl")
postgresProcess := exec.Command(postgresBinary, "start", "-w",
"-D", ep.config.dataPath,
"-o", fmt.Sprintf(`"-p %d"`, ep.config.port))
"-o", encodeOptions(ep.config.port, ep.config.startParameters))
postgresProcess.Stdout = ep.syncedLogger.file
postgresProcess.Stderr = ep.syncedLogger.file

Expand Down
37 changes: 36 additions & 1 deletion embedded_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ func Test_ErrorWhenCannotStartPostgresProcess(t *testing.T) {

err = database.Start()

assert.EqualError(t, err, fmt.Sprintf(`could not start postgres using %s/bin/pg_ctl start -w -D %s/data -o "-p 5432"`, extractPath, extractPath))
assert.EqualError(t, err, fmt.Sprintf(`could not start postgres using %s/bin/pg_ctl start -w -D %s/data -o -p 5432`, extractPath, extractPath))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exec.Command actually ensures each of the arguments are passed directly, so we don't need to wrap the whole options string in quotes - in fact, if you do (as I did initially) it causes problems. See https://stackoverflow.com/a/26473771

}

func Test_CustomConfig(t *testing.T) {
Expand Down Expand Up @@ -354,6 +354,41 @@ func Test_CustomLocaleConfig(t *testing.T) {
}
}

func Test_CustomStartParameters(t *testing.T) {
//database := NewDatabase(DefaultConfig())
database := NewDatabase(DefaultConfig().StartParameters(map[string]string{
"max_connections": "101",
"shared_buffers": "16 MB", // Ensure a parameter with spaces encodes correctly.
}))
if err := database.Start(); err != nil {
shutdownDBAndFail(t, err, database)
}

db, err := sql.Open("postgres", "host=localhost port=5432 user=postgres password=postgres dbname=postgres sslmode=disable")
if err != nil {
shutdownDBAndFail(t, err, database)
}

if err := db.Ping(); err != nil {
shutdownDBAndFail(t, err, database)
}

row := db.QueryRow("SHOW max_connections")
var res string
if err := row.Scan(&res); err != nil {
shutdownDBAndFail(t, err, database)
}
assert.Equal(t, "101", res)

if err := db.Close(); err != nil {
shutdownDBAndFail(t, err, database)
}

if err := database.Stop(); err != nil {
shutdownDBAndFail(t, err, database)
}
}

func Test_CanStartAndStopTwice(t *testing.T) {
database := NewDatabase()

Expand Down