This project implements the PostgreSQL wire protocol in Go using only the standard library.
func main() {
connStr := "postgres://postgres:123456@localhost:5432/postgres"
driveConfig := models.DriveConfig{Verbose: true,}
pgConnection, err := protocol.NewPgConnection(connStr, driveConfig)
if err != nil {
fmt.Println(err)
return
}
userToFind := "postgres"
res, err := pgConnection.Query("SELECT * FROM pg_user WHERE usename = $1;", userToFind)
if err != nil {
fmt.Println(err)
pgConnection.Close()
return
}
fmt.Println("Postgres user: ", res.Rows)
pgConnection.Close()
}
- Flexible Connection Handling
- Supports both URL-style connection strings (postgres://user:pass@host:port/db)
- Supports key-value connection strings (
host=localhost port=5432
)
- SSL/TLS Support
- Automatic SSL/TLS negotiation when
sslmode=require
- Secure encrypted connections
- Automatic SSL/TLS negotiation when
- Query Interface
- Simple query protocol support
- Extended query protocol with parameter binding
- Support for parameterized queries using $1, $2 etc.
- Connection Configuration
- Configurable verbose mode for debugging
- Custom drive configuration options via models.DriveConfig
- Authentication
- SCRAM-SHA-256
- md5
- clear text
- Clean Resource Management
- Proper connection termination
-
Clone the repository:
git clone https://github.com/ViniciusCestarii/postgres-protocol-go.git
-
Create environment file:
cp .env.example .env
-
Set the environment variables in the
.env
file. -
Run the client implementation:
go run cmd/client.go
postgres-protocol-go/
βββ cmd/
β βββ client.go # Client implementation example using this driver
βββ internal/
β βββ pool/ # Buff writer
β βββ protocol/ # PostgreSQL wire protocol handling
βββ pkg/
β βββ utils/ # Shared utilities (logging, errors, helpers)
β βββ models/ # Data structures for queries, results, etc.
βββ tests/ # Integration and unit tests
βββ go.mod # Go module file
βββ README.md # Project documentation
To run the tests, use the following commands:
go test ./tests/...
Official Protocol Documentation
This project is licensed under the MIT License - see the LICENSE file for details.