Difference between revisions of "Embedded postgresql using golang"
Jump to navigation
Jump to search
Line 13: | Line 13: | ||
``` | ``` | ||
− | + | runpg.go | |
``` | ``` | ||
package main | package main | ||
Line 62: | Line 62: | ||
} | } | ||
} | } | ||
+ | ``` | ||
+ | |||
+ | Build | ||
+ | ``` | ||
+ | go build runpg.go | ||
+ | ``` | ||
+ | |||
+ | Run | ||
+ | ``` | ||
+ | ./runpg | ||
``` | ``` |
Revision as of 03:49, 11 March 2022
.env
export PGDATABASE=demo export PGHOST=127.0.0.1 export PGPASSWORD=demo export PGPORT=15432 export PGUSER=demo
get package
go get -u github.com/fergusstrange/embedded-postgres
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