All Projects → broady → gae-postgres

broady / gae-postgres

Licence: Apache-2.0 license
Connect to Cloud SQL for PostgreSQL from Google App Engine

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to gae-postgres

url2epub
Create ePub files from URLs
Stars: ✭ 35 (+52.17%)
Mutual labels:  appengine, appengine-go
aelog
App Engine Logger
Stars: ✭ 24 (+4.35%)
Mutual labels:  appengine, appengine-go
docker-laravel-appengine
Laravel dockerized with official Google App Engine flexible php environment + swoole.
Stars: ✭ 66 (+186.96%)
Mutual labels:  postgres, appengine
gae blog
GAE Blog is a minimalist blog meant to be used with Google App Engine.
Stars: ✭ 16 (-30.43%)
Mutual labels:  appengine
ud859
Udacity course ud859 with Go
Stars: ✭ 18 (-21.74%)
Mutual labels:  appengine
pg-pubsub
Reliable PostgreSQL LISTEN/NOTIFY with inter-process lock support
Stars: ✭ 50 (+117.39%)
Mutual labels:  postgres
pgsink
Logically replicate data out of Postgres into sinks (files, Google BigQuery, etc)
Stars: ✭ 53 (+130.43%)
Mutual labels:  postgres
cloud-sql-python-connector
Cloud SQL Connector for Python
Stars: ✭ 126 (+447.83%)
Mutual labels:  cloud-sql
pg global temp tables
Oracle-style global temporary tables for PostgreSQL
Stars: ✭ 16 (-30.43%)
Mutual labels:  postgres
pg-search-sequelize
Postgres full-text search in Node.js and Sequelize.
Stars: ✭ 31 (+34.78%)
Mutual labels:  postgres
shyft
⬡ Shyft is a server-side framework for building powerful GraphQL APIs 🚀
Stars: ✭ 56 (+143.48%)
Mutual labels:  postgres
IreneBot
Irene Bot for Discord in Python
Stars: ✭ 15 (-34.78%)
Mutual labels:  postgres
clunk
Clojure Postgres w/out JDBC
Stars: ✭ 25 (+8.7%)
Mutual labels:  postgres
phpPgAdmin6
PHP7+ Based administration tool for PostgreSQL 9.3+
Stars: ✭ 45 (+95.65%)
Mutual labels:  postgres
GuideApp
👍 [Android] [Google App Engine] Application that displays the cities services. (Restaurants, hotels, attractions, etc.)
Stars: ✭ 19 (-17.39%)
Mutual labels:  appengine
migrant lib
Embeddable migration management
Stars: ✭ 22 (-4.35%)
Mutual labels:  postgres
Processor
Ontology-driven Linked Data processor and server for SPARQL backends. Apache License.
Stars: ✭ 54 (+134.78%)
Mutual labels:  appengine
event bus postgres
🐘 Postgres event store for event_bus
Stars: ✭ 49 (+113.04%)
Mutual labels:  postgres
postgresql lwrp
Express 42 postgresql cookbook
Stars: ✭ 57 (+147.83%)
Mutual labels:  postgres
rocket-rest-api-with-jwt
A Rusty Rocket fuelled with Diesel and secured by JWT
Stars: ✭ 62 (+169.57%)
Mutual labels:  postgres

Cloud SQL for PostgreSQL on Google App Engine

GoDoc

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

runtime: go
api_version: go1

handlers:
- url: /.*
  script: _go_app

env_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

// 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 main

import (
	"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
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].