Sqlboiler
Fiber with sqlboiler
π― Fiber + Sqlboiler 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 ./migrations -seq create_initial_tablesqlboiler/migrations/000001_create_initial_table.up.sql
sqlboiler/migrations/000001_create_initial_table.down.sql2. Migrate
$ migrate -path migrations -database "postgresql://user:password@localhost:5432/fiber_demo?sslmode=disable" -verbose up2023/09/22 20:00:00 Start buffering 1/u create_initial_table
2023/09/22 20:00:00 Read and execute 1/u create_initial_table
2023/09/22 20:00:00 Finished 1/u create_initial_table (read 24.693541ms, ran 68.30925ms)
2023/09/22 20:00:00 Finished after 100.661625ms
2023/09/22 20:00:00 Closing source and database3. Rollback Migrate
$ migrate -path migrations -database "postgresql://user:password@localhost:5432/fiber_demo?sslmode=disable" -verbose down2023/09/22 20:00:00 Are you sure you want to apply all down migrations? [y/N]
y
2023/09/22 20:00:00 Applying all down migrations
2023/09/22 20:00:00 Start buffering 1/d create_initial_table
2023/09/22 20:00:00 Read and execute 1/d create_initial_table
2023/09/22 20:00:00 Finished 1/d create_initial_table (read 39.681125ms, ran 66.220125ms)
2023/09/22 20:00:00 Finished after 1.83152475s4. Use sqlboiler
1. Install
# Go 1.25 and above:
$ go install github.com/aarondl/sqlboiler/v4@latest
$ go install github.com/aarondl/sqlboiler/v4/drivers/sqlboiler-psql@latestπ―
sqlboilerruns thesqlboiler-psqldriver, so$(go env GOPATH)/binhas to be in yourPATH.
2. Create a configuration file
π― The configuration file should be named sqlboiler.toml
Example
output = "models"
wipe = true
no-tests = true
no-hooks = true
[psql]
dbname = "fiber_demo"
host = "localhost"
port = 5432
user = "user"
pass = "password"
sslmode = "disable"π― The Postgres container has no SSL, and
sqlboilerusessslmode = "require"by default. Withoutsslmode = "disable"it stops withpq: SSL is not enabled on the server.π― Leave
schemaout unless your tables are not in the defaultpublicschema. A schema that does not exist ends withno tables found in database.
3. Create models
π― After creating a configuration file that points at the database we want to generate models for, we can invoke the sqlboiler command line utility.
$ sqlboiler psqlmodels/
βββ author.go
βββ boil_queries.go
βββ boil_table_names.go
βββ boil_types.go
βββ boil_view_names.go
βββ post.go
βββ psql_upsert.go
βββ schema_migrations.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 database6. Try the API
The server listens on port 3000.
| Method | Route | Description |
|---|---|---|
GET | /api/v1/authors | List the authors |
GET | /api/v1/authors/:id | Get an author |
POST | /api/v1/authors | Create an author (name, email) |
PUT | /api/v1/authors/:id | Rename an author (name) |
DELETE | /api/v1/authors/:id | Delete an author who has no posts |
GET | /api/v1/posts | List the posts |
GET | /api/v1/posts/:id | Get a post |
POST | /api/v1/posts | Create a post (title, content, author is the id of an existing author) |
PUT | /api/v1/posts/:id | Update the title and content of 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 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. 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.
$ curl -X POST localhost:3000/api/v1/authors -H 'Content-Type: application/json' -d '{"name":"Ada","email":"ada@example.com"}'{"id":1,"email":"ada@example.com","name":"Ada"}$ curl -X POST localhost:3000/api/v1/posts -H 'Content-Type: application/json' -d '{"title":"Hello","content":"First post","author":1}'{"id":1,"title":"Hello","content":"First post","created_at":"2026-11-15T11:23:08.477667322Z","updated_at":"2026-11-15T11:23:08.477667322Z","author":1}$ curl localhost:3000/api/v1/posts/1{"id":1,"title":"Hello","content":"First post","created_at":"2026-11-15T11:23:08.477667Z","updated_at":"2026-11-15T11:23:08.477667Z","author":1}