recipes/mysql

MySQL + PostgreSQL

Connecting to MySQL or PostgreSQL with database/sql.

MySQL and PostgreSQL Example

Github StackBlitz

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:

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/mysql
  2. Install dependencies:

    go get
  3. Create 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
    );
  4. Update the user, password and dbname constants at the top of main.go to match your database.

    The connection string ends with ?clientFoundRows=true. MySQL normally reports only the rows an UPDATE actually changed, so PUT /employee/:id would answer 404 for an existing employee whose values did not change. With this option the driver reports matched rows, and 0 really means that the employee does not exist.

Running the Application

  1. Start the application:

    go run main.go
  2. The server will start on http://localhost:3000.

Endpoints

MethodURLDescription
GET/employeeRetrieves all employees
GET/employee/:idRetrieves an employee by ID
POST/employeeCreates a new employee
PUT/employee/:idUpdates an existing employee
DELETE/employee/:idDeletes 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/employee

Get an Employee by ID

curl -X GET http://localhost:3000/employee/1

Update 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/1

Using 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 stringroot:password@/fiber_demo?clientFoundRows=truehost=localhost port=5432 user=postgres password=password dbname=fiber_demo sslmode=disable
Query placeholders?$1, $2, ...
ID of an inserted rowresult.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
);

References