MySQL + PostgreSQL
MySQL and PostgreSQL Example
This project demonstrates how to connect to a MySQL database in a Go application using the Fiber framework and the standard library's database/sql package, without an ORM. It exposes a small CRUD API for an employees table. Using PostgreSQL instead only needs a different driver, connection string and placeholder style.
Prerequisites
Ensure you have the following installed:
- Golang
- Fiber package
- MySQL
- Go MySQL Driver
Setup
Clone the repository:
git clone https://github.com/gofiber/recipes.git cd recipes/mysqlInstall dependencies:
go getCreate the database and the table the API works on:
CREATE DATABASE fiber_demo; USE fiber_demo; CREATE TABLE employees ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, salary DOUBLE NOT NULL, age INT NOT NULL );Update the
user,passwordanddbnameconstants at the top ofmain.goto match your database.The connection string ends with
?clientFoundRows=true. MySQL normally reports only the rows anUPDATEactually changed, soPUT /employee/:idwould answer404for an existing employee whose values did not change. With this option the driver reports matched rows, and0really means that the employee does not exist.
Running the Application
Start the application:
go run main.goThe server will start on
http://localhost:3000.
Endpoints
| Method | URL | Description |
|---|---|---|
| GET | /employee | Retrieves all employees |
| GET | /employee/:id | Retrieves an employee by ID |
| POST | /employee | Creates a new employee |
| PUT | /employee/:id | Updates an existing employee |
| DELETE | /employee/:id | Deletes an employee |
Example Requests
Create an Employee
curl -X POST http://localhost:3000/employee -d '{"name": "Jane Doe", "salary": 52000, "age": 31}' -H "Content-Type: application/json"Get All Employees
curl -X GET http://localhost:3000/employeeGet an Employee by ID
curl -X GET http://localhost:3000/employee/1Update an Employee
curl -X PUT http://localhost:3000/employee/1 -d '{"name": "Jane Doe", "salary": 56000, "age": 32}' -H "Content-Type: application/json"Delete an Employee
curl -X DELETE http://localhost:3000/employee/1Using PostgreSQL
The handlers use only database/sql, so switching to PostgreSQL is a matter of changing the driver, the connection string and a few SQL details:
| MySQL (this recipe) | PostgreSQL | |
|---|---|---|
| Driver import | _ "github.com/go-sql-driver/mysql" | _ "github.com/jackc/pgx/v5/stdlib" |
Driver name for sql.Open | "mysql" | "pgx" |
| Connection string | root:password@/fiber_demo?clientFoundRows=true | host=localhost port=5432 user=postgres password=password dbname=fiber_demo sslmode=disable |
| Query placeholders | ? | $1, $2, ... |
| ID of an inserted row | result.LastInsertId() | Add RETURNING id to the INSERT and read it with db.QueryRow(...).Scan(&id). The pgx driver does not support LastInsertId. |
Create the table with PostgreSQL types:
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
salary DOUBLE PRECISION NOT NULL,
age INTEGER NOT NULL
);