Supported PostgreSQL drivers

Supported drivers

pgdriver

Bun comes with its own PostgreSQL driver called pgdriverOverview - 图1open in new window that allows connecting to a PostgreSQL database using a DSN (connection string):

  1. import (
  2. "github.com/uptrace/bun"
  3. "github.com/uptrace/bun/dialect/pgdialect"
  4. "github.com/uptrace/bun/driver/pgdriver"
  5. )
  6. dsn := "postgres://postgres:@localhost:5432/test?sslmode=disable"
  7. // dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
  8. sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
  9. db := bun.NewDB(sqldb, pgdialect.New())

You can specify the following options in a DSN:

  • ?sslmode=verify-full - enable TLS.
  • ?sslmode=disable - disables TLS.
  • ?dial_timeout=5s - timeout for establishing new connections.
  • ?read_timeout=5s - timeout for socket reads.
  • ?write_timeout=5s - timeout for socket writes.
  • ?timeout=5s - sets all three timeouts described above.
  • ?application_name=myapp - PostgreSQL application name.

pgdriver treats all unknown options as PostgreSQL configuration parameters, for example, ?search_path=my_search_path executes the following query whenever a connection is created:

  1. SET search_path TO 'my_search_path'

In addition to DSN, you can also use pgdriver.OptionOverview - 图2open in new window to configure the driver:

  1. pgconn := pgdriver.NewConnector(
  2. pgdriver.WithNetwork("tcp"),
  3. pgdriver.WithAddr("localhost:5437"),
  4. pgdriver.WithTLSConfig(&tls.Config{InsecureSkipVerify: true}),
  5. pgdriver.WithUser("test"),
  6. pgdriver.WithPassword("test"),
  7. pgdriver.WithDatabase("test"),
  8. pgdriver.WithApplicationName("myapp"),
  9. pgdriver.WithTimeout(5 * time.Second),
  10. pgdriver.WithDialTimeout(5 * time.Second),
  11. pgdriver.WithReadTimeout(5 * time.Second),
  12. pgdriver.WithWriteTimeout(5 * time.Second),
  13. pgdriver.WithConnParams(map[string]interface{}{
  14. "search_path": "my_search_path",
  15. }),
  16. )

Or use a DSN and driver options together:

  1. pgconn := pgdriver.NewConnector(
  2. pgdriver.WithDSN("postgres://postgres:@localhost:5432/test?sslmode=verify-full"),
  3. pgdriver.WithTLSConfig(tlsConfig),
  4. )

pgdriver.Error

pgdriver exposes ErrorOverview - 图3open in new window type to work with PostgreSQL errors:

  1. import "github.com/jackc/pgerrcode"
  2. _, err := db.NewInsert().Model(&model).Exec(ctx)
  3. if err != nil {
  4. if err, ok := err.(pgdriver.Error); ok && err.IntegrityViolation() {
  5. // ...
  6. } else if err.Field('C') == pgerrcode.InvalidTransactionState {
  7. // ...
  8. } else {
  9. // ...
  10. }
  11. }

Debugging

If you suspect an issue with pgdriver, try to replace it with pgx and check if the problem goes away.

pgx

As an alternative to pgdriver, you can also use pgxOverview - 图4open in new window with pgdialect. With pgx, you can disable implicit prepared statements, because Bun does not benefit from using them:

  1. import (
  2. "github.com/uptrace/bun"
  3. "github.com/uptrace/bun/dialect/pgdialect"
  4. "github.com/jackc/pgx/v4/stdlib"
  5. )
  6. config, err := pgx.ParseConfig("postgres://postgres:@localhost:5432/test?sslmode=disable")
  7. if err != nil {
  8. panic(err)
  9. }
  10. config.PreferSimpleProtocol = true
  11. sqldb := stdlib.OpenDB(*config)
  12. db := bun.NewDB(sqldb, pgdialect.New())

PgBouncer

To achieve better performance, you can use a server-side connection pool like PgBouncerOverview - 图5open in new window. The pool that comes with sql.DB is a client-side pool and it doesn’t replace a server-side pool provided by PgBouncer.

ZFS

If you store large amounts of data (> 100 gigabytes), consider using ZFS filesystem which enables 2-3x data compression and efficient ARC cache. See: