Golang Database
Samuel BertheCommunityCreated by samberDistributed by Samuel Berthe
- Skill
- Tool integration
- Software development
- MIT
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a set of best practices for interacting with databases in Go using standard libraries like database/sql, sqlx, or pgx. It emphasizes explicit SQL usage over ORMs, focusing on security, performance, and maintainability. The guide covers essential topics including parameterized queries to prevent SQL injection, proper context propagation, transaction management, connection pool configuration, and robust error handling. It is designed for backend engineers who prioritize observable and reliable database code.
What it does
- Provides patterns for secure, parameterized SQL queries to prevent injection.
- Outlines best practices for struct scanning and handling NULLable database columns.
- Defines standards for transaction management and isolation levels.
- Explains how to configure and monitor database connection pools for performance.
- Details proper error handling and context propagation for database operations.
- Offers guidance on choosing between database/sql, sqlx, and pgx.
When to use it
- When writing new repository functions or query helpers in Go.
- When auditing existing Go code for database-related bugs or security vulnerabilities.
- When debugging performance issues related to connection pooling or query execution.
- When determining the best approach for mapping database rows to Go structs.
Advantages
- Promotes explicit, readable SQL that is easier to debug and test.
- Reduces the risk of SQL injection through mandatory parameterization.
- Improves application reliability by enforcing proper resource management like closing rows.
- Helps optimize database performance through correct connection pool settings.
Usage examples
- “How do I safely query a user by email in Go without using an ORM?”
- The agent will provide a code snippet using db.GetContext with a parameterized placeholder ($1 or ?) to ensure the query is safe from SQL injection.
- “Review this code for database connection leaks.”
- The agent will scan the provided code for missing defer rows.Close() calls or improper use of db.Query instead of db.Exec.
- “How should I handle a NULLable column in my database struct?”
- The agent will recommend using pointer fields (e.g., *string) for struct members and explain how they interact with database scanning and JSON marshaling.
- “What are the recommended settings for my database connection pool?”
- The agent will provide a configuration block for SetMaxOpenConns, SetMaxIdleConns, and related methods, explaining the trade-offs for each.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/samber/golang-database/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip golang-database.zip -d .agents/skills
Documentation by samber
Persona: You are a Go backend engineer who writes safe, explicit, and observable database code. You treat SQL as a first-class language — no ORMs, no magic — and you catch data integrity issues at the boundary, not deep in the application.
Modes:
Write mode — generating new repository functions, query helpers, or transaction wrappers: follow the skill's sequential instructions; launch a background agent to grep for existing query patterns and naming conventions in the codebase before generating new code.
Review/debug mode — auditing or debugging existing database code: use a sub-agent to scan for missing
rows.Close(), un-parameterized queries, missing context propagation, and absent error checks in parallel with reading the business logic.
Community default. A company skill that explicitly supersedes
samber/cc-skills-golang@golang-databaseskill takes precedence.
Go Database Best Practices
Go's database/sql provides a solid foundation for database access. Use sqlx or pgx on top of it for ergonomics — never an ORM.
When using sqlx or pgx, refer to the library's official documentation and code examples for current API signatures.
Best Practices Summary
Use sqlx or pgx, not ORMs — ORMs hide SQL, generate unpredictable queries, and make debugging harder
Queries MUST use parameterized placeholders — NEVER concatenate user input into SQL strings
Context MUST be passed to all database operations — use
*Contextmethod variants (QueryContext,ExecContext,GetContext)sql.ErrNoRowsMUST be handled explicitly — distinguish "not found" from real errors usingerrors.IsRows MUST be closed after iteration —
defer rows.Close()immediately afterQueryContextcallsNEVER use
db.Queryfor statements that don't return rows —Queryreturns*Rowswhich must be closed; if you forget, the connection leaks back to the pool. Usedb.ExecinsteadUse transactions for multi-statement operations — wrap related writes in
BeginTxx/CommitUse
SELECT ... FOR UPDATEwhen reading data you intend to modify — prevents race conditionsSet custom isolation levels when default READ COMMITTED is insufficient (e.g., serializable for financial operations)
Handle NULLable columns with pointer fields (
*string,*int) orsql.NullXxxtypesConnection pool MUST be configured —
SetMaxOpenConns,SetMaxIdleConns,SetConnMaxLifetime,SetConnMaxIdleTimeUse external tools for migrations — golang-migrate or Flyway, never hand-rolled or AI-generated migration SQL
Batch operations in reasonable sizes — not row-by-row (too many round trips), not millions at once (locks and memory)
Never create or modify database schemas — a schema that looks correct on toy data can create hotspots, lock contention, or missing indexes under real production load. Schema design requires understanding of data volumes, access patterns, and production constraints that AI does not have
Avoid hidden SQL features — do not rely on triggers, views, materialized views, stored procedures, or row-level security in application code
Library Choice
| Library | Best for | Struct scanning | PostgreSQL-specific |
|---|---|---|---|
database/sql | Portability, minimal deps | Manual Scan | No |
sqlx | Multi-database projects | StructScan | No |
pgx | PostgreSQL (30-50% faster) | pgx.RowToStructByName | Yes (COPY, LISTEN, arrays) |
| GORM/ent | Avoid | Magic | Abstracted away |
Why NOT ORMs:
Unpredictable query generation — N+1 problems you cannot see in code
Magic hooks and callbacks (BeforeCreate, AfterUpdate) make debugging harder
Schema migrations coupled to application code
Learning the ORM API is harder than learning SQL, and the abstraction leaks
Parameterized Queries
// ✗ VERY BAD — SQL injection vulnerability
query := fmt.Sprintf("SELECT * FROM users WHERE email = '%s'", email)
// ✓ Good — parameterized (PostgreSQL)
var user User
err := db.GetContext(ctx, &user, "SELECT id, name, email FROM users WHERE email = $1", email)
// ✓ Good — parameterized (MySQL)
err := db.GetContext(ctx, &user, "SELECT id, name, email FROM users WHERE email = ?", email)Dynamic IN clauses
query, args, err := sqlx.In("SELECT * FROM users WHERE id IN (?)", ids)
if err != nil {
return fmt.Errorf("building IN clause: %w", err)
}
query = db.Rebind(query) // adjust placeholders for your driver
err = db.SelectContext(ctx, &users, query, args...)Dynamic column names
Never interpolate column names from user input. Use an allowlist:
allowed := map[string]bool{"name": true, "email": true, "created_at": true}
if !allowed[sortCol] {
return fmt.Errorf("invalid sort column: %s", sortCol)
}
query := fmt.Sprintf("SELECT id, name, email FROM users ORDER BY %s", sortCol)For more injection prevention patterns, see the samber/cc-skills-golang@golang-security skill.
Struct Scanning and NULLable Columns
Use db:"column_name" tags for sqlx, pgx.CollectRows with pgx.RowToStructByName for pgx. Handle NULLable columns with pointer fields (*string, *time.Time) — they work cleanly with both scanning and JSON marshaling. See Scanning Reference for examples of all approaches.
Error Handling
func GetUser(id string) (*User, error) {
var user User
err := db.GetContext(ctx, &user, "SELECT id, name FROM users WHERE id = $1", id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrUserNotFound // translate to domain error
}
return nil, fmt.Errorf("querying user %s: %w", id, err)
}
return &user, nil
}or:
func GetUser(id string) (u *User, exists bool, err error) {
var user User
err := db.GetContext(ctx, &user, "SELECT id, name FROM users WHERE id = $1", id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, false, nil // "no user" is not a technical error, but a domain error
}
return nil, false, fmt.Errorf("querying user %s: %w", id, err)
}
return &user, true, nil
}Always close rows
rows, err := db.QueryContext(ctx, "SELECT id, name FROM users")
if err != nil {
return fmt.Errorf("querying users: %w", err)
}
defer rows.Close() // prevents connection leaks
for rows.Next() {
// ...
}
if err := rows.Err(); err != nil { // always check after iteration
return fmt.Errorf("iterating users: %w", err)
}Common database error patterns
| Error | How to detect | Action |
|---|---|---|
| Row not found | errors.Is(err, sql.ErrNoRows) | Return domain error |
| Unique constraint | Check driver-specific error code | Return conflict error |
| Connection refused | err != nil on db.PingContext | Fail fast, log, retry with backoff |
| Serialization failure | PostgreSQL error code 40001 | Retry the entire transaction |
| Context canceled | errors.Is(err, context.Canceled) | Stop processing, propagate |
Context Propagation
Always use the *Context method variants to propagate deadlines and cancellation:
// ✗ Bad — no context, query runs until completion even if client disconnects
db.Query("SELECT ...")
// ✓ Good — respects context cancellation and timeouts
db.QueryContext(ctx, "SELECT ...")For context patterns in depth, see the samber/cc-skills-golang@golang-context skill.
Transactions, Isolation Levels, and Locking
For transaction patterns, isolation levels, SELECT FOR UPDATE, and locking variants, see Transactions.
Connection Pool
db.SetMaxOpenConns(25) // limit total connections
db.SetMaxIdleConns(10) // keep warm connections ready
db.SetConnMaxLifetime(5 * time.Minute) // recycle stale connections
db.SetConnMaxIdleTime(1 * time.Minute) // close idle connections fasterFor sizing guidance and formulas, see Database Performance.
Migrations
Use an external migration tool. Schema changes require human review with understanding of data volumes, existing indexes, foreign keys, and production constraints.
Recommended tools:
golang-migrate — CLI + Go library, supports all major databases
Flyway — JVM-based, widely used in enterprise environments
Atlas — modern, declarative schema management
Migration SQL should be written and reviewed by humans, versioned in source control, and applied through CI/CD pipelines.
Avoid Hidden SQL Features
Do not rely on triggers, views, materialized views, stored procedures, or row-level security in application code — they create invisible side effects and make debugging impossible. Keep SQL explicit and visible in Go where it can be tested and version-controlled.
Schema Creation
This skill does NOT cover schema creation. AI-generated schemas are often subtly wrong — missing indexes, incorrect column types, bad normalization, or missing constraints. Schema design requires understanding data volumes, access patterns, query profiles, and business constraints. Use dedicated database tooling and human review.
Deep Dives
Transactions — Transaction boundaries, isolation levels, deadlock prevention,
SELECT FOR UPDATETesting Database Code — Mock connections, integration tests with containers, fixtures, schema setup/teardown
Database Performance — Connection pool sizing, batch processing, indexing strategy, query optimization
Struct Scanning — Struct tags, NULLable column handling, JSON marshaling patterns
Cross-References
→ See
samber/cc-skills-golang@golang-securityskill for SQL injection prevention patterns→ See
samber/cc-skills-golang@golang-contextskill for context propagation to database operations→ See
samber/cc-skills-golang@golang-error-handlingskill for database error wrapping patterns→ See
samber/cc-skills-golang@golang-testingskill for database integration test patterns
References
Files
- ATTRIBUTION.mdAdded by AutoGineer902 B
- LICENSELicence file added by AutoGineer1.0 KiB
- SKILL.md11.3 KiB
- evals/evals.json15.0 KiB
- references/performance.md9.0 KiB
- references/scanning.md2.2 KiB
- references/testing.md6.5 KiB
- references/transactions.md1.5 KiB
references/testing.md
Testing Database Code
Table of Contents
Unit Tests with Mocks
Mock for service-layer tests
sqlmock for Query-Level Testing
Integration Tests
Test database with testcontainers-go
What to Test
Unit Tests with Mocks
Define a repository interface so business logic can be tested without a database. Mock the interface with testify/mock:
// Repository interface — the contract
type UserRepository interface {
GetByID(ctx context.Context, id int64) (*User, bool, error)
Create(ctx context.Context, user *User) error
}
// Production implementation
type pgUserRepository struct {
db *sqlx.DB
}
func (r *pgUserRepository) GetByID(ctx context.Context, id int64) (*User, bool, error) {
var user User
err := r.db.GetContext(ctx, &user, "SELECT id, name, email FROM users WHERE id = $1", id)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, false, nil
}
return nil, false, fmt.Errorf("querying user %d: %w", id, err)
}
return &user, true, nil
}Mock for service-layer tests
type mockUserRepo struct {
mock.Mock
}
func (m *mockUserRepo) GetByID(ctx context.Context, id int64) (*User, error) {
args := m.Called(ctx, id)
if args.Get(0) == nil {
return nil, args.Error(1)
}
return args.Get(0).(*User), args.Error(1)
}
func TestUserService_GetUser(t *testing.T) {
repo := new(mockUserRepo)
svc := NewUserService(repo)
expected := &User{ID: 1, Name: "Alice", Email: "[email protected]"}
repo.On("GetByID", mock.Anything, int64(1)).Return(expected, nil)
user, err := svc.GetUser(context.Background(), 1)
require.NoError(t, err)
assert.Equal(t, expected, user)
repo.AssertExpectations(t)
}
func TestUserService_GetUser_NotFound(t *testing.T) {
repo := new(mockUserRepo)
svc := NewUserService(repo)
repo.On("GetByID", mock.Anything, int64(999)).Return(nil, ErrUserNotFound)
user, err := svc.GetUser(context.Background(), 999)
assert.Nil(t, user)
assert.ErrorIs(t, err, ErrUserNotFound)
}Unit tests verify business logic, not SQL correctness. They run fast and without external dependencies.
sqlmock for Query-Level Testing
When you need to verify exact SQL without a real database, use DATA-DOG/go-sqlmock:
func TestGetByID_sqlmock(t *testing.T) {
db, mock, err := sqlmock.New()
require.NoError(t, err)
defer db.Close()
sqlxDB := sqlx.NewDb(db, "postgres")
repo := &pgUserRepository{db: sqlxDB}
rows := sqlmock.NewRows([]string{"id", "name", "email"}).
AddRow(1, "Alice", "[email protected]")
mock.ExpectQuery("SELECT id, name, email FROM users WHERE id = \\$1").
WithArgs(1).
WillReturnRows(rows)
user, err := repo.GetByID(context.Background(), 1)
require.NoError(t, err)
assert.Equal(t, "Alice", user.Name)
assert.NoError(t, mock.ExpectationsWereMet())
}sqlmock is useful for verifying query structure and error handling paths, but it does not validate that your SQL is correct against a real database schema.
Integration Tests
Integration tests run against a real database. Gate them with build tags so go test ./... skips them by default:
//go:build integration
package repository_test
import (
"testing"
"github.com/stretchr/testify/suite"
)
type UserRepoSuite struct {
suite.Suite
db *sqlx.DB
tx *sqlx.Tx
}
func (s *UserRepoSuite) SetupSuite() {
dsn := os.Getenv("TEST_DATABASE_URL") // e.g., postgres://test:test@localhost:5432/testdb?sslmode=disable
db, err := sqlx.Connect("postgres", dsn)
s.Require().NoError(err)
s.db = db
// Run migrations here if needed
}
func (s *UserRepoSuite) TearDownSuite() {
s.db.Close()
}
func (s *UserRepoSuite) SetupTest() {
tx, err := s.db.Beginx()
s.Require().NoError(err)
s.tx = tx
}
func (s *UserRepoSuite) TearDownTest() {
s.tx.Rollback() // rolls back all changes — each test starts clean
}
func (s *UserRepoSuite) TestCreateAndGet() {
repo := NewUserRepository(s.tx)
user := &User{Name: "Alice", Email: "[email protected]"}
err := repo.Create(context.Background(), user)
s.Require().NoError(err)
s.NotZero(user.ID)
got, err := repo.GetByID(context.Background(), user.ID)
s.Require().NoError(err)
s.Equal("Alice", got.Name)
}
func TestUserRepoSuite(t *testing.T) {
suite.Run(t, new(UserRepoSuite))
}Run integration tests:
go test -tags=integration -v ./internal/repository/...Test database with testcontainers-go
For CI environments without a pre-existing database:
func (s *UserRepoSuite) SetupSuite() {
ctx := context.Background()
container, err := postgres.Run(ctx, "postgres:16-alpine",
postgres.WithDatabase("testdb"),
postgres.WithUsername("test"),
postgres.WithPassword("test"),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(30*time.Second),
),
)
s.Require().NoError(err)
s.container = container
connStr, err := container.ConnectionString(ctx, "sslmode=disable")
s.Require().NoError(err)
s.db, err = sqlx.Connect("postgres", connStr)
s.Require().NoError(err)
}What to Test
| What | Unit test (mock) | Integration test |
|---|---|---|
| Business logic | ✓ | |
| SQL correctness | ✓ | |
| Error paths (not found) | ✓ | ✓ |
| Transaction boundaries | ✓ | |
| NULL handling round-trips | ✓ | |
| Constraint violations | ✓ | |
| Query performance | ✓ (with EXPLAIN) |
Unit tests MUST use mocks (interface mocks or sqlmock) — no real database connections.
Integration tests MUST use build tags (
//go:build integration) to separate from unit tests.Integration tests SHOULD use testcontainers-go for reproducible database environments in CI.
NEVER test against production databases.
→ See samber/cc-skills-golang@golang-testing skill for general test patterns and CI configuration.
Requirements and compatibility
Written for: Claude Code, OpenAI Codex
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Declares allowed tools, Claude Code frontmatter
The author says: Designed for Claude Code, Codex or similar harness, and for projects using Golang.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- samber
- Publisher
- Samuel Berthe (Community)
- Distributed by
- Samuel Berthe
- Source
- samber/cc-skills-golang · skills/golang-database · commit bb1d43d
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by samber or Samuel Berthe.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download golang-database.zip