Sqlc
Fiber with sqlc
π― fiber + sqlc Example
Usage
1. Run Postgres
$ docker compose up -dThe compose file mounts
./data/postgres/at/var/lib/postgresql. Postgres 18 keeps its data in a versioned subdirectory of that path, and a mount at/var/lib/postgresql/datamakes the container exit.
2. Wait until Postgres is ready
The first start takes a few seconds. Check the log, which has to end with the line below:
$ docker compose logs postgresqlpostgres | 2026-11-15 11:23:44.061 UTC [1] LOG: database system is ready to accept connections3. You have to migrate the database.
π― It is a "database-first" ORM as opposed to "code-first" (like gorm/gorp). That means you must first create your database schema.
π― I used golang-migrate to proceed with the migrate.
π― Install the
migratecommand withgo install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest.π― The repository already contains this migration, so you can skip step 1 and run the migration (step 2).
1. Make Migration files
$ migrate create -ext sql -dir ./database/migrations -seq create_initial_tablesqlc/database/migrations/000001_create_initial_table.up.sql
sqlc/database/migrations/000001_create_initial_table.down.sql2. Migrate
$ migrate -path database/migrations -database "postgresql://user:password@localhost:5432/fiber_demo?sslmode=disable" -verbose up2023/09/28 20:00:00 Start buffering 1/u create_initial_table
2023/09/28 20:00:00 Read and execute 1/u create_initial_table
2023/09/28 20:00:00 Finished 1/u create_initial_table (read 24.693541ms, ran 68.30925ms)
2023/09/28 20:00:00 Finished after 100.661625ms
2023/09/28 20:00:00 Closing source and database3. Rollback Migrate
$ migrate -path database/migrations -database "postgresql://user:password@localhost:5432/fiber_demo?sslmode=disable" -verbose down2023/09/28 20:00:00 Are you sure you want to apply all down migrations? [y/N]
y
2023/09/28 20:00:00 Applying all down migrations
2023/09/28 20:00:00 Start buffering 1/d create_initial_table
2023/09/28 20:00:00 Read and execute 1/d create_initial_table
2023/09/28 20:00:00 Finished 1/d create_initial_table (read 39.681125ms, ran 66.220125ms)
2023/09/28 20:00:00 Finished after 1.83152475s4. Use sqlc
1. Install
# Go 1.25 and above:
$ go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest
# Legacy Go versions (<1.25):
go get github.com/sqlc-dev/sqlc/cmd/sqlc2. Create a configuration file
Example
sqlc.yaml
version: "2"
sql:
- engine: "postgresql"
queries: "database/query"
schema: "database/migrations"
gen:
go:
package: "sqlc"
out: "database/sqlc"
# Lower-case JSON keys.
emit_json_tags: true
# Nullable columns as pointers. With the default sql.NullString/sql.NullTime types,
# a column serializes as {"String":"...","Valid":true} and cannot be decoded from
# {"content":"text"}. (emit_pointers_for_null_types would do this too, but it only
# works with the pgx driver, and this recipe uses database/sql.)
overrides:
- column: "post.content"
go_type:
type: "string"
pointer: true
- column: "post.created_at"
go_type:
import: "time"
type: "Time"
pointer: true
- column: "post.updated_at"
go_type:
import: "time"
type: "Time"
pointer: trueauthor.sql
-- name: GetAuthors :many
SELECT * FROM author;
-- name: GetAuthor :one
SELECT * FROM author WHERE id = $1;
-- name: NewAuthor :one
INSERT INTO author (email, name) VALUES ($1, $2) RETURNING *;
-- name: UpdateAuthor :one
UPDATE author SET email = $1, name = $2 WHERE id = $3 RETURNING *;
-- name: DeleteAuthor :exec
DELETE FROM author WHERE id = $1;post.sql
-- name: GetPosts :many
SELECT * FROM post;
-- name: GetPost :one
SELECT * FROM post WHERE id = $1;
-- name: NewPost :one
INSERT INTO post (title, content, author) VALUES ($1, $2, $3) RETURNING *;
-- name: UpdatePost :one
UPDATE post SET title = $1, content = $2, author = $3 WHERE id = $4 RETURNING *;
-- name: DeletePost :exec
DELETE FROM post WHERE id = $1;
3. Generate
$ sqlc generatesqlc/
βββ author.sql.go
βββ db.go
βββ models.go
βββ post.sql.go5. Run the application
π― The application reads the database settings from the environment (it does not read a
.envfile), so load the values of.env.examplefirst.
$ set -a; source .env.example; set +a
$ go run .Connected to databaseThe server listens on port 3000, see the endpoints below.
6. Reference
API Endpoints
All paths are below /api/v1, for example http://localhost:3000/api/v1/posts. JSON keys are lower-case (id, title, created_at, ...), and the nullable content, created_at and updated_at columns are a plain value or null.
Authors
| Method | Path | Description |
|---|---|---|
| GET | /api/v1/authors | List all authors |
| GET | /api/v1/authors/:id | Get an author by ID |
| POST | /api/v1/authors | Create a new author |
| PUT | /api/v1/authors/:id | Update an author |
| DELETE | /api/v1/authors/:id | Delete an author |
Posts
| Method | Path | Description |
|---|---|---|
| GET | /api/v1/posts | List all posts |
| GET | /api/v1/posts/:id | Get a post by ID |
| POST | /api/v1/posts | Create a new post |
| PUT | /api/v1/posts/:id | Update a post |
| DELETE | /api/v1/posts/:id | Delete a post |
The answers to a request that cannot be served are 400 for an id that is not a 32-bit number, 404 when the author or post of a GET or PUT request does not exist, 422 when a post names an author that does not exist, and 409 when you delete an author who still has posts. A DELETE of an id that does not exist is answered with 200 like any other, because the :exec queries do not report whether a row was deleted. Any other failure is a 500 with the text internal server error. The details of the database error are only written to the server log, because the driver messages name tables and constraints.
Example
curl -X POST http://localhost:3000/api/v1/authors -H "Content-Type: application/json" \
-d '{"email": "ann@example.com", "name": "Ann"}'
curl -X POST http://localhost:3000/api/v1/posts -H "Content-Type: application/json" \
-d '{"title": "Hello", "content": "plain text", "author": 1}'
curl http://localhost:3000/api/v1/posts/1