Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/broady/gae-postgres
Connect to Cloud SQL for PostgreSQL from Google App Engine
https://github.com/broady/gae-postgres
appengine appengine-go cloud-sql go golang postgres postgresql
Last synced: 1 day ago
JSON representation
Connect to Cloud SQL for PostgreSQL from Google App Engine
- Host: GitHub
- URL: https://github.com/broady/gae-postgres
- Owner: broady
- License: apache-2.0
- Created: 2017-11-08T06:41:33.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-25T18:23:29.000Z (over 6 years ago)
- Last Synced: 2024-06-20T08:16:29.713Z (5 months ago)
- Topics: appengine, appengine-go, cloud-sql, go, golang, postgres, postgresql
- Language: Go
- Homepage:
- Size: 9.77 KB
- Stars: 23
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Cloud SQL for PostgreSQL on Google App Engine
[GoDoc](https://godoc.org/github.com/broady/gae-postgres)
## Disclaimer
This is not a Google product, and is unsupported.
It uses the `google.golang.org/appengine/cloudsql` package, which is supported for MySQL, but not for Postgres.
It happens to work right now, but may not forever.
## Example
app.yaml
```yaml
runtime: go
api_version: go1handlers:
- url: /.*
script: _go_appenv_variables:
# Replace INSTANCE_CONNECTION_NAME with the value obtained when configuring your
# Cloud SQL instance, available from the Google Cloud Console or from the Cloud SDK.
# For Cloud SQL 2nd generation instances, this should be in the form of "project:region:instance".
CLOUDSQL_CONNECTION_NAME: 'INSTANCE_CONNECTION_NAME'
# Replace username and password if you aren't using the root user.
CLOUDSQL_USER: postgres
CLOUDSQL_PASSWORD: pw
```cloudsql.go
```go
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.// Sample cloudsql_postgres demonstrates connection to a Cloud SQL for Postgres instance from App Engine standard.
package mainimport (
"bytes"
"database/sql"
"fmt"
"log"
"net/http"
"os""google.golang.org/appengine"
_ "github.com/broady/gae-postgres"
)var db *sql.DB
func main() {
var (
connectionName = mustGetenv("CLOUDSQL_CONNECTION_NAME")
user = mustGetenv("CLOUDSQL_USER")
password = os.Getenv("CLOUDSQL_PASSWORD") // NOTE: password may be empty
)var err error
db, err = sql.Open("gae-postgres", fmt.Sprintf("cloudsql=%s user=%s password='%s'", connectionName, user, password))
if err != nil {
log.Fatalf("Could not open db: %v", err)
}http.HandleFunc("/", handler)
appengine.Main()
}func handler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}w.Header().Set("Content-Type", "text/plain")
rows, err := db.Query("SELECT datname FROM pg_database")
if err != nil {
http.Error(w, fmt.Sprintf("Could not query db: %v", err), 500)
return
}
defer rows.Close()buf := bytes.NewBufferString("Databases:\n")
for rows.Next() {
var dbName string
if err := rows.Scan(&dbName); err != nil {
http.Error(w, fmt.Sprintf("Could not scan result: %v", err), 500)
return
}
fmt.Fprintf(buf, "- %s\n", dbName)
}
w.Write(buf.Bytes())
}func mustGetenv(k string) string {
v := os.Getenv(k)
if v == "" {
log.Panicf("%s environment variable not set.", k)
}
return v
}
```