xo/dburl

Package dburl provides a standard, URL style mechanism for parsing and opening SQL database connection strings

Go

298

130 commits

updated Oct 7, 2026

See the code

README

About dburl

Package dburl parses and opens SQL database connection strings for Go, in a standard URL style. It handles the URL formats of PostgreSQL, MySQL, SQLite3, Oracle Database and Microsoft SQL Server. It also handles most other SQL databases that have a public Go driver.

Overview | Quickstart | Examples | Schemes | Installing | Using | About

Unit Tests Go Reference Discord Discussion

Database Connection URL Overview

Supported database connection URLs are of the form:

protocol+transport://user:pass@host/dbname?opt1=a&opt2=b
protocol:/path/to/file

Where:

ComponentDescription
protocoldriver name or alias (see below)
transport"tcp", "udp", "unix", "http", "https", or a driver name for odbc
userusername
passpassword
hosthost
dbname*database, instance, or service name/ID to connect to
?opt1=...additional database driver options (see respective SQL driver for available options)

* For Microsoft SQL Server, /dbname can be /instance/dbname, where /instance is optional. For Oracle Database, /dbname takes the form /service/dbname. Here /service is the service name or SID, and /dbname is optional. See the examples below.

Quickstart

The dburl.Parse func parses a database connection URL in the format above:

import (
    "github.com/xo/dburl"
)

u, err := dburl.Parse("postgresql://user:pass@localhost/mydatabase/?sslmode=disable")
if err != nil { /* ... */ }

dburl.Open parses the URL and returns an open standard sql.DB database connection:

import (
    "github.com/xo/dburl"
)

db, err := dburl.Open("sqlite:mydatabase.sqlite3?loc=auto")
if err != nil { /* ... */ }

Example URLs

dburl.Parse and dburl.Open handle database connection URLs such as these:

postgres://user:pass@localhost/dbname
pg://user:pass@localhost/dbname?sslmode=disable
pq://user:pass@localhost/dbname
mysql://user:pass@localhost/dbname
mysql:/var/run/mysqld/mysqld.sock
sqlserver://user:pass@remote-host.com/dbname
mssql://user:pass@remote-host.com/instance/dbname
ms://user:pass@remote-host.com:port/instance/dbname?keepAlive=10
oracle://user:pass@somehost.com/sid
sap://user:pass@localhost/dbname
cassandra://user:pass@localhost/keyspace?consistency=localQuorum
sqlite:/path/to/file.db
file:myfile.sqlite3?loc=auto
odbc+postgres://user:pass@localhost:port/dbname?option1=

Database Schemes, Aliases, and Drivers

The table lists every supported dburl protocol scheme, with its aliases and its Go driver. A parsed URL names the scheme in URL.SchemeName and the name to pass to sql.Open in URL.Driver. The two differ when a scheme opens a driver that another product also uses: tidb opens mysql, postgres, cockroachdb and redshift open pgx, and pq opens postgres, which is the name that github.com/lib/pq registers. dburl.Open passes URL.Driver to sql.Open:

DatabaseScheme / TagScheme AliasesDriver Package / Notes
PostgreSQLpostgrespg, pgsql, postgresqlgithub.com/jackc/pgx/v5/stdlib
MySQLmysqlmy, maria, aurora, mariadb, perconagithub.com/go-sql-driver/mysql
Microsoft SQL Serversqlserverms, mssql, azuresqlgithub.com/microsoft/go-mssqldb
Oracle Databaseoracleor, ora, oci, oci8, odpi, odpi-cgithub.com/sijms/go-ora/v3
SQLite3sqlite3sq, sqlite, filegithub.com/mattn/go-sqlite3 † §
DuckDBduckdbdk, ddb, duck, filegithub.com/duckdb/duckdb-go/v2 † §
ClickHouseclickhousechgithub.com/xo/dbimp/clickhouse
CSVQcsvqcs, csv, tsv, jsongithub.com/mithrandie/csvq-driver §
Alibaba MaxComputemaxcomputemcgithub.com/aliyun/aliyun-odps-go-sdk/sqldriver
Alibaba Tablestoreotsot, tablestoregithub.com/aliyun/aliyun-tablestore-go-sql-driver
Amazon Redshiftredshiftrsgithub.com/jackc/pgx/v5/stdlib
Apache Avaticaavaticaav, phoenixgithub.com/xo/dbimp/avatica
Apache Drilldrilldlgithub.com/xo/dbimp/drill
Apache Druiddruiddrgithub.com/xo/dbimp/druid
Apache H2h2github.com/jmrobles/h2go
Apache Hivehivehi, hive2github.com/beltran/gohive/v2
Apache Impalaimpalaimgithub.com/sclgo/impala-go
Apache Pinotpinotpigithub.com/xo/dbimp/pinot
Apache Solrsolrsogithub.com/xo/dbimp/solr
ArangoDBarangodbar, arangogithub.com/xo/dbimp/arangodb
AWS Athenaawsathenas3, aws, athenagithub.com/uber/athenadriver/go
Azure CosmosDBcosmoscm, gocosmosgithub.com/btnguyen2k/gocosmos
Cassandracqlca, scy, scylla, datastax, cassandragithub.com/xo/cql
ChaiSQLchaici, genji, chaisqlgithub.com/chaisql/chai §
CockroachDBcockroachdbcr, cdb, crdb, cockroachgithub.com/jackc/pgx/v5/stdlib
Couchbasecouchbasen1, n1qlgithub.com/xo/dbimp/couchbase
CrateDBcratedbct, crategithub.com/jackc/pgx/v5/stdlib
Databenddatabenddd, bendgithub.com/xo/dbimp/databend
Databricksdatabricksbr, brick, bricks, databrickgithub.com/databricks/databricks-sql-go
DynamoDBdynamodbdy, dyn, dynamogithub.com/xo/dbimp/dynamodb
Elasticsearchelasticsearches, elasticgithub.com/xo/dbimp/elasticsearch
Exasolexasolex, exagithub.com/exasol/exasol-driver-go
Firebirdfirebirdsqlfb, firebirdgithub.com/nakagami/firebirdsql
FlightSQLflightsqlfl, flightgithub.com/apache/arrow-go/v18/arrow/flight/flightsql/driver
GizmoSQLgizmosqlgz, gizmogithub.com/apache/arrow-go/v18/arrow/flight/flightsql/driver
GO DRiver for ORaclegodrorgrgithub.com/godror/godror †
Google BigQuerybigquerybqgorm.io/driver/bigquery/driver
Google Spannerspannerspgithub.com/googleapis/go-sql-spanner
InfluxDBinfluxdbin, influxgithub.com/xo/dbimp/influxdb
InfluxDB InfluxQLinfluxqliqgithub.com/xo/dbimp/influxdb
libSQLlibsqlls, tursogithub.com/xo/dbimp/libsql
ModernC SQLite3moderncsqlitemq, modernsqlitemodernc.org/sqlite §
Neo4jneo4jnj, neo, n4jgithub.com/xo/dbimp/neo4j
ODBCodbcodgithub.com/xo/odbc
OpenSearchopensearchos, opengithub.com/xo/dbimp/opensearch
PostgreSQL lib/pqpqlibpqgithub.com/lib/pq
PostgreSQL PGXpgxpxgithub.com/jackc/pgx/v5/stdlib
Prestoprestopr, prestodbgithub.com/xo/dbimp/trino
QuestDBquestdbqsgithub.com/jackc/pgx/v5/stdlib
rqliterqliterqgithub.com/xo/dbimp/rqlite
SAP HANAhdbsa, sap, hana, saphanagithub.com/SAP/go-hdb/driver
SingleStore MemSQLmemsqlmegithub.com/go-sql-driver/mysql
Snowflakesnowflakesfgithub.com/snowflakedb/gosnowflake/v2
SurrealDBsurrealdbsr, sur, surrealgithub.com/xo/dbimp/surrealdb
TiDBtidbtigithub.com/go-sql-driver/mysql
Trinotrinotrgithub.com/xo/dbimp/trino
Verticaverticavegithub.com/vertica/vertica-sql-go
Vitess Databasevitessvtgithub.com/go-sql-driver/mysql
VoltDBvoltdbvo, vdb, voltgithub.com/VoltDB/voltdb-client-go/voltdbclient
YDBydbydgithub.com/ydb-platform/ydb-go-sdk/v3

† Requires CGO
§ Embedded, with no server to run
Hosted service, with no server you can run

You can write any alias as alias:// in place of protocol://. dburl.Parse and dburl.Open treat the two the same.

Installing

Install dburl with go get:

$ go get github.com/xo/dburl@latest

Using

dburl does not import any Go SQL driver. It only parses and opens database connection URLs, so you must import the SQL driver yourself:

import (
    // import Microsoft SQL Server driver
    _ "github.com/microsoft/go-mssqldb"
)

See the database schemes table above for the Go driver that each scheme expects.

The dburl package documentation has more examples and the API details.

URL Parsing Rules

dburl.Parse and dburl.Open build on Go's standard net/url.URL type. The same rules and conventions apply as for Go's net/url.Parse func.

Example

A full example:

// _example/example.go
package main

import (
	"fmt"
	"log"

	_ "github.com/microsoft/go-mssqldb"
	"github.com/xo/dburl"
)

func main() {
	db, err := dburl.Open("sqlserver://user:pass@localhost/dbname")
	if err != nil {
		log.Fatal(err)
	}
	var name string
	if err := db.QueryRow(`SELECT name FROM mytable WHERE id=10`).Scan(&name); err != nil {
		log.Fatal(err)
	}
	fmt.Println("name:", name)
}

Scheme Resolution

On systems other than Windows, dburl resolves a path on disk, or a URL with a file: scheme, to a database driver:

  1. A directory resolves as a postgres: URL.
  2. A Unix socket resolves as a mysql: URL.
  3. For a file that exists, dburl reads the file header and resolves it as a sqlite3: or a duckdb: URL.
  4. For a file that does not exist, dburl matches the file extension against the known sqlite3: and duckdb: extensions.

To turn this off, set dburl.ResolveSchemeType to false. You can also supply your own dburl.Stat and dburl.OpenFile funcs instead:

import "github.com/xo/dburl"

func init() {
    dburl.ResolveSchemeType = false
}

About

dburl exists to support these projects:

  • usql - a universal command-line interface for SQL databases
  • dbtpl - a command-line tool to generate code for SQL databases
  • dbmeta - a Go package that reads metadata from SQL databases

xo/dburl

Package dburl provides a standard, URL style mechanism for parsing and opening SQL database connection strings

Go

298

130 commits

updated Oct 7, 2026

See the code

README

About dburl

Package dburl parses and opens SQL database connection strings for Go, in a standard URL style. It handles the URL formats of PostgreSQL, MySQL, SQLite3, Oracle Database and Microsoft SQL Server. It also handles most other SQL databases that have a public Go driver.

Overview | Quickstart | Examples | Schemes | Installing | Using | About

Unit Tests Go Reference Discord Discussion

Database Connection URL Overview

Supported database connection URLs are of the form:

protocol+transport://user:pass@host/dbname?opt1=a&opt2=b
protocol:/path/to/file

Where:

ComponentDescription
protocoldriver name or alias (see below)
transport"tcp", "udp", "unix", "http", "https", or a driver name for odbc
userusername
passpassword
hosthost
dbname*database, instance, or service name/ID to connect to
?opt1=...additional database driver options (see respective SQL driver for available options)

* For Microsoft SQL Server, /dbname can be /instance/dbname, where /instance is optional. For Oracle Database, /dbname takes the form /service/dbname. Here /service is the service name or SID, and /dbname is optional. See the examples below.

Quickstart

The dburl.Parse func parses a database connection URL in the format above:

import (
    "github.com/xo/dburl"
)

u, err := dburl.Parse("postgresql://user:pass@localhost/mydatabase/?sslmode=disable")
if err != nil { /* ... */ }

dburl.Open parses the URL and returns an open standard sql.DB database connection:

import (
    "github.com/xo/dburl"
)

db, err := dburl.Open("sqlite:mydatabase.sqlite3?loc=auto")
if err != nil { /* ... */ }

Example URLs

dburl.Parse and dburl.Open handle database connection URLs such as these:

postgres://user:pass@localhost/dbname
pg://user:pass@localhost/dbname?sslmode=disable
pq://user:pass@localhost/dbname
mysql://user:pass@localhost/dbname
mysql:/var/run/mysqld/mysqld.sock
sqlserver://user:pass@remote-host.com/dbname
mssql://user:pass@remote-host.com/instance/dbname
ms://user:pass@remote-host.com:port/instance/dbname?keepAlive=10
oracle://user:pass@somehost.com/sid
sap://user:pass@localhost/dbname
cassandra://user:pass@localhost/keyspace?consistency=localQuorum
sqlite:/path/to/file.db
file:myfile.sqlite3?loc=auto
odbc+postgres://user:pass@localhost:port/dbname?option1=

Database Schemes, Aliases, and Drivers

The table lists every supported dburl protocol scheme, with its aliases and its Go driver. A parsed URL names the scheme in URL.SchemeName and the name to pass to sql.Open in URL.Driver. The two differ when a scheme opens a driver that another product also uses: tidb opens mysql, postgres, cockroachdb and redshift open pgx, and pq opens postgres, which is the name that github.com/lib/pq registers. dburl.Open passes URL.Driver to sql.Open:

DatabaseScheme / TagScheme AliasesDriver Package / Notes
PostgreSQLpostgrespg, pgsql, postgresqlgithub.com/jackc/pgx/v5/stdlib
MySQLmysqlmy, maria, aurora, mariadb, perconagithub.com/go-sql-driver/mysql
Microsoft SQL Serversqlserverms, mssql, azuresqlgithub.com/microsoft/go-mssqldb
Oracle Databaseoracleor, ora, oci, oci8, odpi, odpi-cgithub.com/sijms/go-ora/v3
SQLite3sqlite3sq, sqlite, filegithub.com/mattn/go-sqlite3 † §
DuckDBduckdbdk, ddb, duck, filegithub.com/duckdb/duckdb-go/v2 † §
ClickHouseclickhousechgithub.com/xo/dbimp/clickhouse
CSVQcsvqcs, csv, tsv, jsongithub.com/mithrandie/csvq-driver §
Alibaba MaxComputemaxcomputemcgithub.com/aliyun/aliyun-odps-go-sdk/sqldriver
Alibaba Tablestoreotsot, tablestoregithub.com/aliyun/aliyun-tablestore-go-sql-driver
Amazon Redshiftredshiftrsgithub.com/jackc/pgx/v5/stdlib
Apache Avaticaavaticaav, phoenixgithub.com/xo/dbimp/avatica
Apache Drilldrilldlgithub.com/xo/dbimp/drill
Apache Druiddruiddrgithub.com/xo/dbimp/druid
Apache H2h2github.com/jmrobles/h2go
Apache Hivehivehi, hive2github.com/beltran/gohive/v2
Apache Impalaimpalaimgithub.com/sclgo/impala-go
Apache Pinotpinotpigithub.com/xo/dbimp/pinot
Apache Solrsolrsogithub.com/xo/dbimp/solr
ArangoDBarangodbar, arangogithub.com/xo/dbimp/arangodb
AWS Athenaawsathenas3, aws, athenagithub.com/uber/athenadriver/go
Azure CosmosDBcosmoscm, gocosmosgithub.com/btnguyen2k/gocosmos
Cassandracqlca, scy, scylla, datastax, cassandragithub.com/xo/cql
ChaiSQLchaici, genji, chaisqlgithub.com/chaisql/chai §
CockroachDBcockroachdbcr, cdb, crdb, cockroachgithub.com/jackc/pgx/v5/stdlib
Couchbasecouchbasen1, n1qlgithub.com/xo/dbimp/couchbase
CrateDBcratedbct, crategithub.com/jackc/pgx/v5/stdlib
Databenddatabenddd, bendgithub.com/xo/dbimp/databend
Databricksdatabricksbr, brick, bricks, databrickgithub.com/databricks/databricks-sql-go
DynamoDBdynamodbdy, dyn, dynamogithub.com/xo/dbimp/dynamodb
Elasticsearchelasticsearches, elasticgithub.com/xo/dbimp/elasticsearch
Exasolexasolex, exagithub.com/exasol/exasol-driver-go
Firebirdfirebirdsqlfb, firebirdgithub.com/nakagami/firebirdsql
FlightSQLflightsqlfl, flightgithub.com/apache/arrow-go/v18/arrow/flight/flightsql/driver
GizmoSQLgizmosqlgz, gizmogithub.com/apache/arrow-go/v18/arrow/flight/flightsql/driver
GO DRiver for ORaclegodrorgrgithub.com/godror/godror †
Google BigQuerybigquerybqgorm.io/driver/bigquery/driver
Google Spannerspannerspgithub.com/googleapis/go-sql-spanner
InfluxDBinfluxdbin, influxgithub.com/xo/dbimp/influxdb
InfluxDB InfluxQLinfluxqliqgithub.com/xo/dbimp/influxdb
libSQLlibsqlls, tursogithub.com/xo/dbimp/libsql
ModernC SQLite3moderncsqlitemq, modernsqlitemodernc.org/sqlite §
Neo4jneo4jnj, neo, n4jgithub.com/xo/dbimp/neo4j
ODBCodbcodgithub.com/xo/odbc
OpenSearchopensearchos, opengithub.com/xo/dbimp/opensearch
PostgreSQL lib/pqpqlibpqgithub.com/lib/pq
PostgreSQL PGXpgxpxgithub.com/jackc/pgx/v5/stdlib
Prestoprestopr, prestodbgithub.com/xo/dbimp/trino
QuestDBquestdbqsgithub.com/jackc/pgx/v5/stdlib
rqliterqliterqgithub.com/xo/dbimp/rqlite
SAP HANAhdbsa, sap, hana, saphanagithub.com/SAP/go-hdb/driver
SingleStore MemSQLmemsqlmegithub.com/go-sql-driver/mysql
Snowflakesnowflakesfgithub.com/snowflakedb/gosnowflake/v2
SurrealDBsurrealdbsr, sur, surrealgithub.com/xo/dbimp/surrealdb
TiDBtidbtigithub.com/go-sql-driver/mysql
Trinotrinotrgithub.com/xo/dbimp/trino
Verticaverticavegithub.com/vertica/vertica-sql-go
Vitess Databasevitessvtgithub.com/go-sql-driver/mysql
VoltDBvoltdbvo, vdb, voltgithub.com/VoltDB/voltdb-client-go/voltdbclient
YDBydbydgithub.com/ydb-platform/ydb-go-sdk/v3

† Requires CGO
§ Embedded, with no server to run
Hosted service, with no server you can run

You can write any alias as alias:// in place of protocol://. dburl.Parse and dburl.Open treat the two the same.

Installing

Install dburl with go get:

$ go get github.com/xo/dburl@latest

Using

dburl does not import any Go SQL driver. It only parses and opens database connection URLs, so you must import the SQL driver yourself:

import (
    // import Microsoft SQL Server driver
    _ "github.com/microsoft/go-mssqldb"
)

See the database schemes table above for the Go driver that each scheme expects.

The dburl package documentation has more examples and the API details.

URL Parsing Rules

dburl.Parse and dburl.Open build on Go's standard net/url.URL type. The same rules and conventions apply as for Go's net/url.Parse func.

Example

A full example:

// _example/example.go
package main

import (
	"fmt"
	"log"

	_ "github.com/microsoft/go-mssqldb"
	"github.com/xo/dburl"
)

func main() {
	db, err := dburl.Open("sqlserver://user:pass@localhost/dbname")
	if err != nil {
		log.Fatal(err)
	}
	var name string
	if err := db.QueryRow(`SELECT name FROM mytable WHERE id=10`).Scan(&name); err != nil {
		log.Fatal(err)
	}
	fmt.Println("name:", name)
}

Scheme Resolution

On systems other than Windows, dburl resolves a path on disk, or a URL with a file: scheme, to a database driver:

  1. A directory resolves as a postgres: URL.
  2. A Unix socket resolves as a mysql: URL.
  3. For a file that exists, dburl reads the file header and resolves it as a sqlite3: or a duckdb: URL.
  4. For a file that does not exist, dburl matches the file extension against the known sqlite3: and duckdb: extensions.

To turn this off, set dburl.ResolveSchemeType to false. You can also supply your own dburl.Stat and dburl.OpenFile funcs instead:

import "github.com/xo/dburl"

func init() {
    dburl.ResolveSchemeType = false
}

About

dburl exists to support these projects:

  • usql - a universal command-line interface for SQL databases
  • dbtpl - a command-line tool to generate code for SQL databases
  • dbmeta - a Go package that reads metadata from SQL databases

Significant stargazers

Matt Johnson-Pint

430 followers · starred Jan 2025

Stephan Renatus

136 followers · starred Nov 2024

Kujtim Hoxha

572 followers · starred Mar 2017

Michael Fridman

265 followers · starred Nov 2021