Embedded postgresql using golang
Jump to navigation
Jump to search
nano .env
export PGDATABASE=demo export PGHOST=127.0.0.1 export PGPASSWORD=demo export PGPORT=15432 export PGUSER=demo
Source
. .env
Get package
go get -u github.com/fergusstrange/embedded-postgres
nano runpg.go
package main /* https://pkg.go.dev/github.com/fergusstrange/embedded-postgres#section-readme https://github.com/fergusstrange/embedded-postgres 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(15432). StartTimeout(15 * time.Second). Logger(logger) postgres := embeddedpostgres.NewDatabase(conf) err := postgres.Start() if err != nil { fmt.Println(err) } fmt.Printf("Running postgres server for %d seconds.", runseconds) time.Sleep(time.Duration(runseconds) * time.Second) err = postgres.Stop() if err != nil { fmt.Println(err) } }
Build
go build runpg.go
Run
./runpg