Skip to content

UUID Support

Jack Christensen edited this page Sep 17, 2022 · 2 revisions

The Go standard library does not have a type directly corresponding to PostgreSQL uuid. 3rd party libraries are available but pgx does not use them by default to avoid external dependencies.

However, pgx supports both the ability to register new data types and to override the default handling of currently supported types. There is a packages available to use github.com/gofrs/uuid.

Import the following package:

import (
	pgxuuid "github.com/jackc/pgx-gofrs-uuid"
)

Then register the data type for your connection. If you are using a connection pool this should be done in the AfterConnect hook.

dbconfig, err := pgxpool.ParseConfig(databaseURL)
if err != nil {
	// handle error
}
dbconfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
	pgxuuid.Register(conn.TypeMap())
	return nil
}
Clone this wiki locally