- Pull the database from Docker Hub:
docker pull postgres:15.3-alpine3.18
- Run the database container:
sudo docker run --name postgres15.3 -p 9876:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=1903 -d postgres:15.3-alpine3.18
- Login to the database:
sudo docker exec -it postgres15.3 psql -U root
- Create the
simple_bank
database:
CREATE DATABASE simple_bank OWNER root;
- Import the
.sql
file into thesimple_bank
database.
You can also perform database migration automatically without doing it manually using Go Database migrations. First, make sure you have pulled the appropriate database image:
docker pull postgres:15.3-alpine3.18
Then, install the required library for Go Database migrations:
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
Use the provided Makefile to simplify execution.
-
Install SQLC.
-
Install Go Postgres driver for database/sql.
- Change directory to your project path:
cd /to/your/path/project
- Initialize SQLC:
sqlc init
- Set the configuration in
sqlc.yaml
.
Implement queries account.sql, entry.sql, transfer.sql .
-
Install testify for testing.
-
Implement main_test.go to connect to the database.
-
Implement account_test.go, entry_test.go, transfer_test to test the functions.
-
Additionally, you can use random_generate.go to generate random data for testing purposes.