fergusstrange / embedded-postgres

Run a real Postgres database locally on Linux, OSX or Windows as part of another Go application or test

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issues changing defaults via DefaultConfig()

jeremybusk opened this issue · comments

I'm trying to change the defaults via DefaultConfig and is not working as per README.
Maybe you can point me in the right direction.

go version
go version go1.17.1 linux/amd64
package main

import (
  "bytes"
  "fmt"
  "time"

  "github.com/fergusstrange/embedded-postgres"
)

func main() {
  V12 := "12.10.0"
  logger := &bytes.Buffer{}
  postgres := embeddedpostgres.NewDatabase(DefaultConfig().
        Username("beer").
        Password("wine").
        Database("gin").
        Version(V12).
        RuntimePath("/tmp").
        BinaryRepositoryURL("https://repo1.maven.org/maven2").
        Port(9876).
        StartTimeout(45 * time.Second).
        Logger(logger))
  err := postgres.Start()
  fmt.Println(err)
}
go build a.go
# command-line-arguments
./a.go:14:44: undefined: DefaultConfig

Any idea what I'm doing wrong? Thanks! Great project BTW!

Got it figured out. Here is some example code that for those interested.

package main

/*
https://github.com/fergusstrange/embedded-postgres
https://pkg.go.dev/github.com/fergusstrange/embedded-postgres#section-readme
https://github.com/zonkyio/embedded-postgres
https://mvnrepository.com/artifact/io.zonky.test.postgres/embedded-postgres-binaries-bom
*/

import (
        "bytes"
        "fmt"
        "time"

        "github.com/fergusstrange/embedded-postgres"
)

func main() {
        runseconds := 60
        // Version("14.2.0").
        // Version("13.6.0").
        // Version("12.10.0").
        logger := &bytes.Buffer{}
        conf := embeddedpostgres.DefaultConfig().
                Username("demo").
                Password("demo").
                Database("demo").
                Version("14.2.0").
                RuntimePath("./pgdata").
                BinaryRepositoryURL("https://repo1.maven.org/maven2").
                Port(9999).
                StartTimeout(15 * time.Second).
                Logger(logger)

        postgres := embeddedpostgres.NewDatabase(conf)
        err := postgres.Start()
        fmt.Println(err)
        fmt.Printf("Running postgres server for %d seconds.", runseconds)
        time.Sleep(time.Duration(runseconds) * time.Second)

        errstop := postgres.Stop()
        fmt.Println(errstop)
}

Alas, that looks like I've missed some important documentation. Thanks for this @jeremybusk I'll get the README updated.