Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

alextanhongpin/postgres

Repository files navigation

postgres

A simple wrapper around basic operations with Postgres in Golang.

Usage

Creating a new connection string:

cs := ConnString{
	Host: "127.0.0.1",
	Port: 5342,
	User: "john",
	Password: "123456",
	Database: "test",
	Params: map[string]string{
		"sslmode": "disable",  // Automatically added.
	},
}
connStr := cs.String()

Reading from environment variables

This reads the environment variables DB_HOST, DB_PORT, DB_NAME, DB_USER and DB_PASS. It will return an error if any of that is missing.

cs, err := NewConnString()
if err != nil {
	log.Fatal(err)
}
connStr := cs.String()

New DB Instance

Creates a new database instance:

db, err := New(cs.String())
if err != nil {
	log.Fatal(err)
}
defer db.Close()

It also takes additional params to define the migrations source using packr2, so that you can run the migration when the application starts:

New(cs.String(), WithMigrationsPath("./migrations"))

Testing in Docker

See here.

About

Wrapper of common utilities used when using Postgres in Go

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages