|
| 1 | +(connect-go)= |
| 2 | + |
| 3 | +# Go |
| 4 | + |
| 5 | +:::{include} /_include/links.md |
| 6 | +::: |
| 7 | + |
| 8 | +:::{div} sd-text-muted |
| 9 | +Use pgx to connect to CrateDB from Go applications. |
| 10 | +::: |
| 11 | + |
| 12 | +(go-pgx)= |
| 13 | +## pgx |
| 14 | + |
| 15 | +:::{rubric} About |
| 16 | +::: |
| 17 | + |
| 18 | +[pgx] is a pure Go driver and toolkit for PostgreSQL. |
| 19 | + |
| 20 | +:::{rubric} Synopsis |
| 21 | +::: |
| 22 | + |
| 23 | +`go.mod` |
| 24 | +```text |
| 25 | +module github.com/cratedb-guide/connect/go/pgx |
| 26 | +require github.com/jackc/pgx/v5 v5.7.6 |
| 27 | +``` |
| 28 | +`example.go` |
| 29 | +```go |
| 30 | +package main |
| 31 | + |
| 32 | +import ( |
| 33 | + "context" |
| 34 | + "fmt" |
| 35 | + |
| 36 | + "github.com/jackc/pgx/v5" |
| 37 | +) |
| 38 | + |
| 39 | +func main() { |
| 40 | + ctx := context.Background() |
| 41 | + |
| 42 | + // Connect to database. |
| 43 | + conn, _ := pgx.Connect(ctx, "postgresql://crate:crate@localhost:5432/doc?sslmode=disable") |
| 44 | + defer conn.Close(ctx) |
| 45 | + |
| 46 | + // Invoke basic query. |
| 47 | + rows, _ := conn.Query(ctx, "SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3") |
| 48 | + defer rows.Close(ctx) |
| 49 | + |
| 50 | + // Display results. |
| 51 | + for rows.Next() { |
| 52 | + var mountain string |
| 53 | + var height int |
| 54 | + rows.Scan(&mountain, &height) |
| 55 | + fmt.Println(mountain, height) |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +:::{include} ../_cratedb.md |
| 61 | +::: |
| 62 | +```shell |
| 63 | +go mod tidy |
| 64 | +go run example.go |
| 65 | +``` |
| 66 | + |
| 67 | +:::{rubric} CrateDB Cloud |
| 68 | +::: |
| 69 | + |
| 70 | +For connecting to CrateDB Cloud, use `sslmode=require`, and |
| 71 | +replace username, password, and hostname with values matching |
| 72 | +your environment. |
| 73 | +```go |
| 74 | +conn, _ := pgx.Connect(ctx, "postgresql://admin:password@testcluster.cratedb.net:5432/doc?sslmode=require") |
| 75 | +``` |
| 76 | + |
| 77 | +:::{rubric} Example |
| 78 | +::: |
| 79 | + |
| 80 | +:::{card} |
| 81 | +:link: https://github.com/crate/cratedb-examples/tree/main/by-language/go-pgx |
| 82 | +:link-type: url |
| 83 | +{material-outlined}`play_arrow;2em` |
| 84 | +Connect to CrateDB and CrateDB Cloud using Go. |
| 85 | ++++ |
| 86 | +Demonstrates basic examples and bulk insert operations using the pgx driver. |
| 87 | +::: |
| 88 | + |
| 89 | +[](https://github.com/crate/cratedb-examples/actions/workflows/lang-go-pgx.yml) |
| 90 | + |
| 91 | + |
| 92 | +(go-pq)= |
| 93 | +## pq |
| 94 | + |
| 95 | +:::{rubric} About |
| 96 | +::: |
| 97 | + |
| 98 | +[pq] is a pure Go PostgreSQL driver for Go's database/sql package. |
| 99 | + |
| 100 | +:::{rubric} Synopsis |
| 101 | +::: |
| 102 | + |
| 103 | +`go.mod` |
| 104 | +```text |
| 105 | +module github.com/cratedb-guide/connect/go/pq |
| 106 | +require github.com/lib/pq v1.10.9 |
| 107 | +``` |
| 108 | +`example.go` |
| 109 | +```go |
| 110 | +package main |
| 111 | + |
| 112 | +import ( |
| 113 | + "database/sql" |
| 114 | + "fmt" |
| 115 | + |
| 116 | + _ "github.com/lib/pq" |
| 117 | +) |
| 118 | + |
| 119 | +func main() { |
| 120 | + |
| 121 | + // Connect to database. |
| 122 | + connStr := "postgresql://crate:crate@localhost:5432/doc?sslmode=disable" |
| 123 | + db, _ := sql.Open("postgres", connStr) |
| 124 | + defer db.Close() |
| 125 | + |
| 126 | + // Invoke basic query. |
| 127 | + rows, _ := db.Query("SELECT mountain, height FROM sys.summits ORDER BY height DESC LIMIT 3") |
| 128 | + defer rows.Close() |
| 129 | + |
| 130 | + // Display results. |
| 131 | + for rows.Next() { |
| 132 | + var mountain string |
| 133 | + var height int |
| 134 | + rows.Scan(&mountain, &height) |
| 135 | + fmt.Println(mountain, height) |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +:::{include} ../_cratedb.md |
| 141 | +::: |
| 142 | +```shell |
| 143 | +go mod tidy |
| 144 | +go run example.go |
| 145 | +``` |
| 146 | + |
| 147 | +:::{rubric} CrateDB Cloud |
| 148 | +::: |
| 149 | + |
| 150 | +For connecting to CrateDB Cloud, use `sslmode=require`, and |
| 151 | +replace username, password, and hostname with values matching |
| 152 | +your environment. |
| 153 | +```go |
| 154 | +connStr := "postgresql://admin:password@testcluster.cratedb.net:5432/doc?sslmode=require" |
| 155 | +``` |
| 156 | + |
| 157 | + |
| 158 | +[pgx]: https://github.com/jackc/pgx |
| 159 | +[pq]: https://github.com/lib/pq |
0 commit comments