protoc-gen-pgdb
A Protocol Buffers code generator plugin that generates PostgreSQL database code from protobuf definitions. This tool allows you to define your database schema, indexes, and queries using protobuf annotations, and then generates the necessary Go code to interact with PostgreSQL databases.
protoc-gen-pgdb bridges the gap between Protocol Buffers and PostgreSQL by automatically generating type-safe database access code. It supports:
- Mapping protobuf messages to PostgreSQL tables
- Creating and managing indexes
- Support for various PostgreSQL data types
- Query building
- Go 1.23 or higher
- Protocol Buffers compiler
- Buf (recommended for code generation)
go install github.com/ductone/protoc-gen-pgdb@latestOr build from source:
git clone https://github.com/ductone/protoc-gen-pgdb.git
cd protoc-gen-pgdb
make buildThe pgdb.v1.msg extension provides options for configuring how a protobuf message maps to a PostgreSQL table:
disabled: Disables code generation for this messageindexes: Defines database indexestenant_id_field: Specifies the field to use for multi-tenancynested_only: Indicates that this message should only be used as a nested typepartitioned: Enables table partitioningpartitioned_by_created_at: Partitions by the created_at timestamppartitioned_by_date_range: Specifies the date range for partitioningstats: Configures PostgreSQL statistics collection
The pgdb.v1.options extension provides options for configuring how a protobuf field maps to a PostgreSQL column:
full_text_type: Configures full-text search type (NONE, EXACT, ENGLISH)full_text_weight: Sets the weight for full-text search (A, B, C, D)message_behavior: Configures how message fields are stored (JSONB, etc.)
See the example directory for complete examples of how to use protoc-gen-pgdb.
make buildmake testmake example- protoc-gen-star: Framework for building Protobuf code generators
- pgx: PostgreSQL driver and toolkit for Go
- goqu: SQL builder for Go
See the LICENSE file for details.