tiny-url-ex/storage/postgres.go

37 lines
619 B
Go

package storage
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "postgres"
password = "@#postgres-passWoRD#@"
dbname = "aicratest"
)
var psqlInfo = fmt.Sprintf("host=%s port=%d user=%s password='%s' dbname=%s sslmode=disable", host, port, user, password, dbname)
// New returns new postgres connection.
func New() (*sql.DB, error) {
conn, err := sql.Open("postgres", psqlInfo)
if err != nil {
return nil, err
}
// defer conn.Close()
// actually connect
err = conn.Ping()
if err != nil {
return nil, err
}
return conn, nil
}