package main import ( "database/sql" "fmt" _ "github.com/lib/pq" "log" "net/http" ) var db *sql.DB const ( dbhost = "DBHOST" dbport = "DBPORT" dbuser = "DBUSER" dbpass = "DBPASS" dbname = "DBNAME" ) func main() { initDb() defer db.Close() http.HandleFunc("/get-data", UserGetHandler) http.HandleFunc("/post-data", UserPostHandler) fmt.Println("Listening on :5000") log.Fatal(http.ListenAndServe(":5000", nil)) } func initDb() { config := dbConfig() var err error psqlInfo := fmt.Sprintf("postgres://%v:%v@%v:%v/%v?sslmode=disable", config[dbuser], config[dbpass], config[dbhost], config[dbport], config[dbname]) fmt.Println(psqlInfo) db, err = sql.Open("postgres", psqlInfo) if err != nil { panic(err) } err = db.Ping() if err != nil { panic(err) } fmt.Println("Successfully connected!") } func dbConfig() map[string]string { conf := make(map[string]string) conf[dbhost] = "POC-golang-postgres-docker_db_1" conf[dbport] = "5432" conf[dbuser] = "root_user" conf[dbpass] = "shahzad12" conf[dbname] = "db_genity" return conf }