Good Practices For Connecting Go Server To Postgres
Published: 2024-05-25 15:46:41


## Preparing a PG DB and Go backend
Lets Create Bookstore Database where we fetch book details

Creating a BookStore DB in [PostgreSQL](https://www.postgresql.org/) 

I'll start with the installation 
```bash
$ sudo apt install postgresql
$ sudo -u postgres psql
```

### Logging into postgres
```bash
psql -h localhost -U postgres

```
Postgres console will be activated
```bash
psql (14.11 (Ubuntu 14.11-0ubuntu0.22.04.1))
Type "help" for help.
postgres=# 
```
Create DB name **bookstore** by this command
```sql
postgres=CREATE DATABASE bookstore;
CREATE DATABASE
```
### Check out the DB and create Table
```sql
postgres=# \c bookstore
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
You are now connected to the database "bookstore" as user "postgres".
```
Create a Table in the bookstore DB
```sql
CREATE TABLE books (
    isbn char(14) NOT NULL,
    title varchar(255) NOT NULL,
    author varchar(255) NOT NULL,
    price decimal(5,2) NOT NULL
);

INSERT INTO books (isbn, title, author, price) VALUES
('9780099470464', 'A House for Mr. Biswas', 'V. S. Naipaul', 8.99),
('9780143037346', 'Miss New India', 'Bharati Mukherjee', 9.99),
('9781784782781', 'The Lives of Others', 'Neel Mukherjee', 11.99),

ALTER TABLE books ADD PRIMARY KEY (isbn);
```
Then Verify whether it is created 
```bash
bookstore-# \dt
         List of relations
 Schema | Name  | Type  |  Owner   
--------+-------+-------+----------
 public | books | table | postgres
(1 row)
```
### Setting up Go backend
```bash
$ mkdir bookstore && cd bookstore
$ mkdir models
$ touch main.go models/models.go
$ go mod init go.backend
go: creating new go.mod: module go.backend
```

File Structure of example :

```bash
bookstore/
├── go.mod
├── go.sum
├── main.go
└── models
    └── models.go
```
## Set a Global DB Instance 

This method simplifies accessing the database connection across the application.

It initializes the database connection, sets up an HTTP server, and listens for incoming requests.

In the context of our bookstore application, the code would look something like this

```go
// models/models.go
package models

import (
    "database/sql"
)
```
Create an exported global variable to hold the database connection pool.

```go
// models/models.go
var DB *sql.DB

type Book struct {
    Isbn   string
    Title  string
    Author string
    Price  float32
}
```
AllBooks() returns a slice of all books in the books table.
```go
// models/models.go
func AllBooks() ([]Book, error) {
    // Note that we are calling Query() on the global variable.
    rows, err := DB.Query("SELECT * FROM books")
    if err != nil {
        return nil, err
    }
    defer rows.Close()

    var bks []Book

    for rows.Next() {
        var bk Book

        err := rows.Scan(&bk.Isbn, &bk.Title, &bk.Author, &bk.Price)
        if err != nil {
            return nil, err
        }

        bks = append(bks, bk)
    }
    if err = rows.Err(); err != nil {
        return nil, err
    }

    return bks, nil
}              
```
```go
// main.go
package main

import (
    "database/sql"
    "fmt"
    "log"
    "net/http"

    "go.backend/models"

    _ "github.com/lib/pq"
)

```
Install dependencies
```bash
$ go get github.com/lib/pq
```
Initialize the `sql.DB` connection pool and assign it to the models.DB 
```go
func main() {
    var err error
    // global variable.
    models.DB, err = sql.Open("postgres", "postgres://user:pass@localhost/bookstore")
    if err != nil {
        log.Fatal(err)
    }

    http.HandleFunc("/books", booksIndex)
    http.ListenAndServe(":3000", nil)
}
```
When I make a request **books**
```bash
$ curl localhost:3000/books
978-1503261969, Emma, Jayne Austen, £9.44
978-1505255607, The Time Machine, H. G. Wells, £5.99
978-1503379640, The Prince, Niccolò Machiavelli, £6.99
```


**Remember** global variable for the database connection is suitable when:
- Your project is simple and small, so tracking global variables isn't hard.
- Your code handling web requests is split across different folders, but all database actions stay in one folder.
- You don't need to pretend the database isn't there for testing.

### Global variable with an `InitDB` function

A variation on the 'global variable' approach that I sometimes see uses an initialization function to set up the connection pool, like so:
- All database stuff is in one place.
- The global database variable is hidden from other parts of the program, so it can't be changed by mistake.
- During testing, you can easily set up a test database connection using a special function.
 
```go
// models/models.go
package models

import (
    "database/sql"

    _ "github.com/lib/pq"
)
```
Initialize the `sql.DB` connection pool and assign it to the models.DB 
```go
// This time the global variable is unexported.
var db *sql.DB

// InitDB sets up setting up the connection pool global variable.
func InitDB(dataSourceName string) error {
    var err error

    db, err = sql.Open("postgres", dataSourceName)
    if err != nil {
        return err
    }

    return db.Ping()
}
```
[Continue Reading](https://journal.hexmos.com/good-practices-for-connecting-go-server-to-the-postgres/#wrapping-the-connection-pool)