- Go 1.25+ - Install Go
- PostgreSQL 16+ - Install PostgreSQL
- Docker & Docker Compose (optional, for containerized development)
-
Clone the repository
git clone https://github.com/justyn-clark/go-chi-postgres-starter.git cd go-chi-postgres-starter -
Install Go dependencies
go mod tidy
-
Set up environment variables
cp .env.example .env # Edit .env with your configuration -
Create database
createdb go-chi-postgres-starter # Or: psql -c 'CREATE DATABASE go_api_starter;' -
Install migration tool
go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest -
Run migrations
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/go_api_starter?sslmode=disable" make migrate-up
-
Start the server
make run
-
Access the API
- API: http://localhost:8080
- Swagger UI: http://localhost:8080/swagger/index.html
-
Start services
docker compose up -d --build
-
Run migrations (first time only)
docker compose exec api sh -c "migrate -path migrations -database \$DATABASE_URL up"
-
Access the API
- API: http://localhost:8080
- Swagger UI: http://localhost:8080/swagger/index.html
For production, generate a secure JWT secret:
openssl rand -base64 32Add it to your .env file:
JWT_SECRET=your-generated-secret-here
make migrate-create NAME=add_user_tableThis creates two files:
migrations/XXXXXX_add_user_table.up.sql- Migration to applymigrations/XXXXXX_add_user_table.down.sql- Migration to rollback
make migrate-upmake migrate-downmake migrate-statusmake swaggerThis generates OpenAPI documentation from code annotations. Visit http://localhost:8080/swagger/index.html after starting the server.
# Run all tests
make test
# Run tests with coverage
make test-coverage- Connect your GitHub repository
- Add PostgreSQL service
- Set environment variables:
DATABASE_URL(auto-provided by Railway)JWT_SECRET(generate withopenssl rand -base64 32)ENVIRONMENT=production
- Deploy!
# Build image
docker build -t go-chi-postgres-starter .
# Run container
docker run -p 8080:8080 \
-e DATABASE_URL=postgresql://... \
-e JWT_SECRET=... \
go-chi-postgres-starter- Ensure PostgreSQL is running
- Check
DATABASE_URLformat:postgresql://user:password@host:port/dbname?sslmode=disable - Verify database exists:
psql -l
- Ensure migrations are in correct order
- Check database connection
- Verify migration files are valid SQL
- Change
PORTin.envfile - Or stop the process using port 8080