| 1 |
--- |
| 2 |
title: "MySQL/PostgreSQL" |
| 3 |
date: 2019-06-21T09:26:56-07:00 |
| 4 |
showInSidenav: true |
| 5 |
toc: true |
| 6 |
--- |
| 7 |
|
| 8 |
Connecting to Cloud providers' hosted database services requires additional |
| 9 |
steps to ensure the security of the connection. For example, each of GCP, |
| 10 |
AWS and Azure require the use of custom certificate authorities to be |
| 11 |
configured in the client. GCP requires a custom proxy with authentication |
| 12 |
credentials. The Go CDK makes opening these connections easier while still |
| 13 |
using the standard [`*sql.DB`][] type. |
| 14 |
|
| 15 |
[`*sql.DB`]: https://godoc.org/database/sql#DB |
| 16 |
|
| 17 |
<!--more--> |
| 18 |
|
| 19 |
## Local or On-Premise {#local} |
| 20 |
|
| 21 |
The Go CDK uses the same [URL opener pattern][] as seen in other Go CDK APIs. It |
| 22 |
differs from the standard library's `sql.Open` in that it automatically |
| 23 |
instruments the connection with [OpenCensus metrics][]. |
| 24 |
|
| 25 |
The portable function for MySQL is [`mysql.Open`][]: |
| 26 |
|
| 27 |
{{< goexample "gocloud.dev/mysql.ExampleOpen" >}} |
| 28 |
|
| 29 |
And the portable function for PostgreSQL is [`postgres.Open`][]: |
| 30 |
|
| 31 |
{{< goexample "gocloud.dev/postgres.ExampleOpen" >}} |
| 32 |
|
| 33 |
[`mysql.Open`]: https://godoc.org/gocloud.dev/mysql#Open |
| 34 |
[OpenCensus metrics]: https://opencensus.io/integrations/sql/go_sql/ |
| 35 |
[`postgres.Open`]: https://godoc.org/gocloud.dev/postgres#Open |
| 36 |
[URL opener pattern]: {{< ref "/concepts/urls.md" >}} |
| 37 |
|
| 38 |
## GCP {#gcp} |
| 39 |
|
| 40 |
Users of [GCP Cloud SQL for MySQL][] should import the `gocloud.dev/mysql/gcpmysql` package: |
| 41 |
|
| 42 |
{{< goexample "gocloud.dev/mysql/gcpmysql.Example" >}} |
| 43 |
|
| 44 |
Users of [GCP Cloud SQL for PostgreSQL][] should import the `gocloud.dev/postgres/gcppostgres` package: |
| 45 |
|
| 46 |
{{< goexample "gocloud.dev/postgres/gcppostgres.Example" >}} |
| 47 |
|
| 48 |
[GCP Cloud SQL for MySQL]: https://cloud.google.com/sql/docs/mysql/ |
| 49 |
[GCP Cloud SQL for PostgreSQL]: https://cloud.google.com/sql/docs/postgres/ |
| 50 |
|
| 51 |
## AWS {#aws} |
| 52 |
|
| 53 |
Users of [AWS RDS for MySQL][] should import the `gocloud.dev/mysql/awsmysql` package: |
| 54 |
|
| 55 |
{{< goexample "gocloud.dev/mysql/awsmysql.Example" >}} |
| 56 |
|
| 57 |
Users of [AWS RDS for PostgreSQL][] should import the `gocloud.dev/postgres/awspostgres` package: |
| 58 |
|
| 59 |
{{< goexample "gocloud.dev/postgres/awspostgres.Example" >}} |
| 60 |
|
| 61 |
[AWS RDS for MySQL]: https://aws.amazon.com/rds/mysql/ |
| 62 |
[AWS RDS for PostgreSQL]: https://aws.amazon.com/rds/postgresql/ |
| 63 |
|
| 64 |
## Azure {#azure} |
| 65 |
|
| 66 |
Users of [Azure Database for MySQL][] should import the `gocloud.dev/mysql/azuremysql` package: |
| 67 |
|
| 68 |
{{< goexample "gocloud.dev/mysql/azuremysql.Example" >}} |
| 69 |
|
| 70 |
[Azure Database for MySQL]: https://azure.microsoft.com/en-us/services/mysql/ |
| 71 |
|
| 72 |
## Other Usage Samples |
| 73 |
|
| 74 |
* [Guestbook sample](https://gocloud.dev/tutorials/guestbook/) |