Type-safe PostgreSQL schema to Go code generator
Transform your PostgreSQL database schemas into elegant, type-safe Go code with zero boilerplate.
- 🔒 Type Safety - Generate strongly-typed Go aliases from PostgreSQL columns
- 🚀 Zero Boilerplate - Automatic code generation with minimal configuration
- 📦 Package Organization - Clean, structured output with proper Go conventions
- 🎯 Column Mapping - Easy-to-use column name constants for query building
- 🔄 Incremental Updates - Regenerate only when schema changes
- 🛡️ Null Safety - Proper handling of nullable columns with pointer types
go install github.com/mymyka/tables/cmd/tables@latesttables --db "host=localhost port=5432 user=postgres password=postgres dbname=mydb sslmode=disable" --output gen/tablesCREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
google_user_id VARCHAR(100) UNIQUE,
hashed_password VARCHAR(255)
);package users
import "time"
// Type aliases for compile-time safety
type Id = int32
type Username = string
type FirstName = string
type LastName = string
type CreatedAt = time.Time
type Email = string
type GoogleUserId = *string // Nullable
type HashedPassword = *string // Nullable
// Column name constants
type usersColumnNames struct {
Id string
Username string
FirstName string
LastName string
CreatedAt string
Email string
GoogleUserId string
HashedPassword string
}
var C = usersColumnNames{
Id: "id",
Username: "username",
FirstName: "first_name",
LastName: "last_name",
CreatedAt: "created_at",
Email: "email",
GoogleUserId: "google_user_id",
HashedPassword: "hashed_password",
}
var Table = "users"import "your-project/gen/tables/users"
type UserModel struct {
Id users.Id
Username users.Username
FirstName users.FirstName
LastName users.LastName
Email users.Email
CreatedAt users.CreatedAt
}// Using column constants prevents typos
query := fmt.Sprintf(`
SELECT %s, %s, %s
FROM %s
WHERE %s = $1
`,
users.C.Id,
users.C.Username,
users.C.Email,
users.Table,
users.C.Username,
)
// Type-safe scanning
var user UserModel
err := db.QueryRow(query, "john_doe").Scan(
&user.Id,
&user.Username,
&user.Email,
)// Nullable fields are properly typed as pointers
var googleId users.GoogleUserId
if user.GoogleUserId != nil {
googleId = *user.GoogleUserId
}| Flag | Description | Required | Default |
|---|---|---|---|
--db |
PostgreSQL connection string | ✅ | - |
--output |
Output directory for generated code | ✅ | - |
--exclude |
Comma-separated list of tables to exclude | ❌ | - |
--include |
Comma-separated list of tables to include | ❌ | All tables |
--package-prefix |
Prefix for generated package names | ❌ | - |
host=localhost port=5432 user=username password=password dbname=database sslmode=disable
After running Tables, your project structure will look like:
your-project/
├── gen/
│ └── tables/
│ ├── users/
│ │ └── users.go
│ ├── orders/
│ │ └── orders.go
│ └── products/
│ └── products.go
├── main.go
└── go.mod
| PostgreSQL Type | Go Type | Nullable Go Type |
|---|---|---|
SERIAL, INTEGER |
int32 |
*int32 |
BIGSERIAL, BIGINT |
int64 |
*int64 |
VARCHAR, TEXT |
string |
*string |
BOOLEAN |
bool |
*bool |
TIMESTAMP |
time.Time |
*time.Time |
DATE |
time.Time |
*time.Time |
DECIMAL, NUMERIC |
float64 |
*float64 |
UUID |
string |
*string |
JSONB |
[]byte |
*[]byte |
We welcome contributions! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by the need for type-safe database interactions in Go
- Built with ❤️ for the Go community
- Special thanks to all contributors
🌊 From Future Pirate King 🌊