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
Supported database connection URLs are of the form:
protocol+transport://user:pass@host/dbname?opt1=a&opt2=b
protocol:/path/to/file
Where:
| Component | Description |
|---|---|
| protocol | driver name or alias (see below) |
| transport | "tcp", "udp", "unix" or driver name (odbc/oleodbc) |
| user | username |
| pass | password |
| host | host |
| 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.
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 { /* ... */ }dburl.Parse and dburl.Open handle database
connection URLs such as these:
postgres://user:pass@localhost/dbname
pg://user:pass@localhost/dbname?sslmode=disable
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
sqlite:/path/to/file.db
file:myfile.sqlite3?loc=auto
odbc+postgres://user:pass@localhost:port/dbname?option1=
The table lists every supported dburl protocol scheme, which is also the
driver name, with its aliases and its Go driver:
| Database | Scheme / Tag | Scheme Aliases | Driver Package / Notes |
|---|---|---|---|
| PostgreSQL | postgres |
pg, pgsql, postgresql |
github.com/lib/pq |
| MySQL | mysql |
my, maria, aurora, mariadb, percona |
github.com/go-sql-driver/mysql |
| Microsoft SQL Server | sqlserver |
ms, mssql, azuresql |
github.com/microsoft/go-mssqldb |
| Oracle Database | oracle |
or, ora, oci, oci8, odpi, odpi-c |
github.com/sijms/go-ora/v3 |
| SQLite3 | sqlite3 |
sq, sqlite, file |
github.com/mattn/go-sqlite3 † |
| DuckDB | duckdb |
dk, ddb, duck, file |
github.com/duckdb/duckdb-go/v2 † |
| ClickHouse | clickhouse |
ch |
github.com/ClickHouse/clickhouse-go/v2 |
| CSVQ | csvq |
cs, csv, tsv, json |
github.com/mithrandie/csvq-driver |
| Alibaba MaxCompute | maxcompute |
mc |
sqlflow.org/gomaxcompute |
| Alibaba Tablestore | ots |
ot, tablestore |
github.com/aliyun/aliyun-tablestore-go-sql-driver |
| Apache Avatica | avatica |
av, phoenix |
github.com/apache/calcite-avatica-go/v5 |
| Apache H2 | h2 |
github.com/jmrobles/h2go | |
| Apache Hive | hive |
hi, hive2 |
sqlflow.org/gohive |
| Apache Ignite | ignite |
ig, gridgain |
github.com/amsokol/ignite-go-client/sql |
| Apache Impala | impala |
im |
github.com/sclgo/impala-go |
| AWS Athena | athena |
s3, aws, awsathena |
github.com/uber/athenadriver/go |
| Azure CosmosDB | cosmos |
cm, gocosmos |
github.com/btnguyen2k/gocosmos |
| Cassandra | cassandra |
ca, scy, scylla, datastax, cql |
github.com/MichaelS11/go-cql-driver |
| ChaiSQL | chai |
ci, genji, chaisql |
github.com/chaisql/chai |
| Couchbase | couchbase |
n1, n1ql |
github.com/couchbase/go_n1ql |
| Cznic QL | ql |
cznic, cznicql |
modernc.org/ql |
| Databend | databend |
dd, bend |
github.com/datafuselabs/databend-go |
| Databricks | databricks |
br, brick, bricks, databrick |
github.com/databricks/databricks-sql-go |
| DynamoDb | dynamodb |
dy, dyn, dynamo, dynamodb |
github.com/btnguyen2k/godynamo |
| Exasol | exasol |
ex, exa |
github.com/exasol/exasol-driver-go |
| Firebird | firebird |
fb, firebirdsql |
github.com/nakagami/firebirdsql |
| FlightSQL | flightsql |
fl, flight |
github.com/apache/arrow/go/v17/arrow/flight/flightsql/driver |
| Google BigQuery | bigquery |
bq |
gorm.io/driver/bigquery/driver |
| Google Spanner | spanner |
sp |
github.com/googleapis/go-sql-spanner |
| Microsoft ADODB | adodb |
ad, ado |
github.com/mattn/go-adodb |
| ModernC SQLite3 | moderncsqlite |
mq, modernsqlite |
modernc.org/sqlite |
| MySQL MyMySQL | mymysql |
zm, mymy |
github.com/ziutek/mymysql/godrv |
| Netezza | netezza |
nz, nzgo |
github.com/IBM/nzgo/v12 |
| PostgreSQL PGX | pgx |
px |
github.com/jackc/pgx/v5/stdlib |
| Presto | presto |
pr, prs, prestos, prestodb, prestodbs |
github.com/prestodb/presto-go-client/v2 |
| SAP ASE | sapase |
ax, ase, tds |
github.com/thda/tds |
| SAP HANA | saphana |
sa, sap, hana, hdb |
github.com/SAP/go-hdb/driver |
| Snowflake | snowflake |
sf |
github.com/snowflakedb/gosnowflake/v2 |
| Trino | trino |
tr, trs, trinos |
github.com/trinodb/trino-go-client/trino |
| Vertica | vertica |
ve |
github.com/vertica/vertica-sql-go |
| VoltDB | voltdb |
vo, vdb, volt |
github.com/VoltDB/voltdb-client-go/voltdbclient |
| YDB | ydb |
yd, yds, ydbs |
github.com/ydb-platform/ydb-go-sdk/v3 |
| GO DRiver for ORacle | godror |
gr |
github.com/godror/godror † |
| ODBC | odbc |
od |
github.com/alexbrainman/odbc † |
| Amazon Redshift | postgres |
rs, redshift |
github.com/lib/pq ‡ |
| CockroachDB | postgres |
cr, cdb, crdb, cockroach, cockroachdb |
github.com/lib/pq ‡ |
| OLE ODBC | adodb |
oo, ole, oleodbc |
github.com/mattn/go-adodb ‡ |
| SingleStore MemSQL | mysql |
me, memsql |
github.com/go-sql-driver/mysql ‡ |
| TiDB | mysql |
ti, tidb |
github.com/go-sql-driver/mysql ‡ |
| Vitess Database | mysql |
vt, vitess |
github.com/go-sql-driver/mysql ‡ |
† Requires CGO
‡ Wire compatible (see respective driver)
You can write any alias as alias:// in place of protocol://.
dburl.Parse and dburl.Open treat the two the
same.
Install dburl with go get:
$ go get github.com/xo/dburl@latestdburl 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.
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.
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)
}On systems other than Windows, dburl resolves a path on disk, or a URL with
a file: scheme, to a database driver:
- A directory resolves as a
postgres:URL. - A Unix socket resolves as a
mysql:URL. - For a file that exists,
dburlreads the file header and resolves it as asqlite3:or aduckdb:URL. - For a file that does not exist,
dburlmatches the file extension against the knownsqlite3:andduckdb: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
}dburl exists to support these projects: