Golang Postgres
In this article, We will show you how to connect to the Postgres Database using Golang.

We are using the following framework and libraries for this article
- Echo Framework (Highly Scalable API development framework)
- GORM (GoLang Object Relation Mapping/Database Connectivity)
Video Tutorial
Golang Configuration
In our case, we have configured the Golang path in the .bash_profile file using MacOS as following
export GOPATH=$HOME/go
export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
In the go directory, we have a folder called github.com

Inside the github.com directory, we have our GitHub user directory called oxlb as showing the following screenshot


We will create a new GoLang project under the oxlb directory
Create a Golang Project
Go to your go configuration directory and create a new directory called

GoLangPostgresHelloWorld
Open the terminal and change directory to GoLangPostgresHelloWorld
$ cd GoLangPostgresHelloWorld
Then create a go.mod file using the following file
$ go mod init github.com/oxlb/GoLangPostgresHelloWorld
this will create the go.mod file as shown below
The above file contains the module and version of the GoLang
Docker Compose Postgres And PGAdmin 4
We are using docker-compose to use Postgres and PGAdmin 4 for the database connectivity test.
If you need more help to understand you can see our previous article for Docker Compose Postgres
Create SQL Script File docker_postgres_init.sql
Create docker-compose.yml file
above file, we have created Postgres and PGAdmin Docker Services
if you see the docker-compose.yml file line no. 13–14 there is a volumes script there as shown below

The above script will create a students table during docker startup and insert 3 records in the table
- A
- B
- C
Run Docker Container
Make sure you have installed Docker on your Machine/System
We have created the video tutorial for Docker Installation below
Try to run the following command to run the container
docker-compose up
Add Echo Framework Dependency using the following command
go get github.com/labstack/echo/v4
Add GORM Dependency using the following command
go get -u gorm.io/gorm
Now you will see your go.mod file looks like the below file
Create db.go
Create new file under config directory /config/db.go
GoLangPostgresHelloWorld
|-- docker-compose.yml (File)
|-- docker_postgres_init.sql (File)
|-- config
|--- db.go

Create a student.go, Model
Create new file under model directory /model/student.go
GoLangPostgresHelloWorld
|-- docker-compose.yml (File)
|-- docker_postgres_init.sql (File)
|-- config
|-- db.go
|-- model
|-- student.go

From the above model, we will fetch Id and Name from the database table students
Create a db.go
Create new file under storage directory /storage/db.go
GoLangPostgresHelloWorld
|-- docker-compose.yml (File)
|-- docker_postgres_init.sql (File)
|-- config
|-- db.go
|-- model
|-- student.go
|-- storage
|-- db.go

above file, we have two different method
- NewDB: To initialize database connection
- GetDBInstance: to get the DB instance
Create a student.go
Create new file under controller directory /controller/student.go
GoLangPostgresHelloWorld
|-- docker-compose.yml (File)
|-- docker_postgres_init.sql (File)
|-- config
|-- db.go
|-- controller
|-- student.go
|-- model
|-- student.go
|-- storage
|-- db.go

Above file, we have created 2 methods
- GetStudents: to use as a route to display the student’s records as a JSON
- GetRepoStudents: to fetch the records from the table students
Create a main.go
Create a new file under GoLangPostgresHelloWorld directory /GoLangPostgresHelloWorld/main.go
GoLangPostgresHelloWorld
|-- docker-compose.yml (File)
|-- docker_postgres_init.sql (File)
|-- config
|-- db.go
|-- controller
|-- student.go
|-- model
|-- student.go
|-- storage
|-- db.go
|-- main.go

Above file, you will see we have two Routes
- e.GET(“/”, hello): will print Hello, world
- e.GET(“/students”, controller.GetStudents): will print
Run Main.go
To run the main file type the following command
go run main.go
you will see it will show you server is running on port:1323

If you try to open the following URL in the Web browser
http://localhost:1323/students

It will return you the database records from the student’s table
Github
Thank you 🚀🚀🚀🚀🚀🚀
