Skip to content
Projects

About

PostgreSQL server wire protocol. Build your own server and start serving connections.

Data & Analytics

How these counts are measured

Measured star growth

in 24h
0
in 7 days
0
in 15 days
-1
in 1 month
-1

Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.

README.md

Original

PSQL wire protocol 🔌

CI Go Reference Latest release Go Report Card

A pure Go PostgreSQL server wire protocol implementation. Build your own PostgreSQL server within a few lines of code. This project attempts to make it as straight forward as possible to set-up and configure your own PSQL server. Feel free to check out the examples directory for various ways on how to configure/set-up your own server.

You can use this package to build your own fully fledged, PostgreSQL compatible database, or simply play with the wire protocol by creating a PSQL honeypot, testing drivers, or experimenting with third-party integrations.

It’s designed to give you a high-level implementation out of the box, while staying hackable so you can bend it to whatever you want to build.

🚧 This project does not include a PSQL parser. Please check out other projects such as auxten/postgresql-parser to parse PSQL SQL queries.

package main

import (
	"context"
	"fmt"

	wire "github.com/jeroenrinzema/psql-wire"
)

func main() {
	wire.ListenAndServe("127.0.0.1:5432", handler)
}

func handler(ctx context.Context, query wire.Query) (wire.PreparedStatements, error) {
	return wire.Prepared(wire.NewStatement(func(ctx context.Context, writer wire.DataWriter, parameters []wire.Parameter) error {
		fmt.Println(query.Query)
		return writer.Complete("OK")
	})), nil
}

Session Attributes

You can store custom session attributes for each client connection, allowing you to track session state:

// Set a session attribute
wire.SetAttribute(ctx, "tenant_id", "tenant-123")

// Get a session attribute
tenantID, ok := wire.GetAttribute(ctx, "tenant_id")

Writing materialized rows

Use wire.WriteRows(writer, rows) to write a [][]any result in order. It uses the optional RowWriter interface when available and otherwise calls Row for each row, so existing DataWriter implementations remain compatible.

For unlimited Execute requests, the built-in writer groups complete DataRow frames into chunks with a 32 KiB flush threshold. Limited portals continue to write and yield per row, preserving portal suspension. Streaming handlers can continue calling Row as values become available.


🚧 When wanting to debug issues and or inspect the PostgreSQL wire protocol please check out the psql-proxy cli

Support

Feel free to start a new discussion to discuss feature requests or issues.

Used by

Cloudproud      Shopify

Contributing

Thank you for your interest in contributing to psql-wire! Check out the open projects and/or issues and feel free to join any ongoing discussion. Feel free to checkout the open TODO's within the project.

Everyone is welcome to contribute, whether it's in the form of code, documentation, bug reports, feature requests, or anything else. We encourage you to experiment with the project and make contributions to help evolve it to meet your needs!

See the contributing guide for more details.

Explore related projects

Loading…