PostgreSQL database sql connection

Run a SQL INSERT statement to open and close a connection to Cloud SQL for PostgreSQL by using Go's database/sql package.

Documentation pages that include this code sample

To view the code sample used in context, see the following documentation:

Code sample


sqlInsert := "INSERT INTO votes(candidate, created_at, updated_at) VALUES($1, NOW(), NOW())"
if team == "TABS" || team == "SPACES" {
	if _, err := app.db.Exec(sqlInsert, team); err != nil {
		fmt.Fprintf(w, "unable to save vote: %s", err)
		return fmt.Errorf("DB.Exec: %v", err)
	fmt.Fprintf(w, "Vote successfully cast for %s!\n", team)
return nil

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.