Skip to content

Commit 0c1d156

Browse files
committed
Driver/Go: Add information about lib/pq
1 parent 438376d commit 0c1d156

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

docs/connect/go.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,71 @@ Demonstrates basic examples and bulk insert operations using the pgx driver.
9090
[![Go pgx CI](https://github.com/crate/cratedb-examples/actions/workflows/lang-go-pgx.yml/badge.svg)](https://github.com/crate/cratedb-examples/actions/workflows/lang-go-pgx.yml)
9191

9292

93+
(go-pq)=
94+
## pq
95+
96+
:::{rubric} About
97+
:::
98+
99+
[pq] is a pure Go PostgreSQL driver for Go's database/sql package.
100+
101+
:::{rubric} Synopsis
102+
:::
103+
104+
`go.mod`
105+
```text
106+
module github.com/cratedb-guide/connect/go/pq
107+
require github.com/lib/pq v1.10.9
108+
```
109+
`example.go`
110+
```go
111+
package main
112+
113+
import (
114+
"database/sql"
115+
"fmt"
116+
117+
_ "github.com/lib/pq"
118+
)
119+
120+
func main() {
121+
122+
// Connect to database.
123+
connStr := "postgresql://crate:crate@localhost/doc?sslmode=disable"
124+
db, _ := sql.Open("postgres", connStr)
125+
126+
// Invoke basic query.
127+
rows, _ := db.Query("SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3")
128+
129+
// Display results.
130+
for rows.Next() {
131+
var mountain string
132+
var height int
133+
rows.Scan(&mountain, &height)
134+
fmt.Println(mountain, height)
135+
}
136+
}
137+
```
138+
139+
Start CrateDB using Docker or Podman, then invoke the example program.
140+
```shell
141+
docker run --rm --publish=5432:5432 crate -Cdiscovery.type=single-node
142+
```
143+
```shell
144+
go mod tidy
145+
go run example.go
146+
```
147+
148+
:::{rubric} CrateDB Cloud
149+
:::
150+
151+
For connecting to CrateDB Cloud, use `sslmode=require`, and
152+
replace username, password, and hostname with values matching
153+
your environment.
154+
```go
155+
connStr := "postgresql://admin:[email protected]:5432/doc?sslmode=require"
156+
```
157+
158+
93159
[pgx]: https://github.com/jackc/pgx
160+
[pq]: https://github.com/lib/pq

0 commit comments

Comments
 (0)