Files
teleport/docs/pages/includes/machine-id/postgresql/postgresql.go
T
22c92bc003 Added Machine ID guide for database access from custom apps (#13226)
* Added Machine ID and Web Apps Guide.

Co-authored-by: Tim Buckley <tim@goteleport.com>

* Update reference pages with latest v9.3 content

Also, update the mongodb example

* Address review feedback

* Remove unfinished though and fix a few typos

* Tweak wording for the webapps guide

Also, fix broken lints in the go examples.

* Fix Postgres example

* Rename "webapps" guide to "database" guide (with custom app)

* Apply suggestions from code review

Co-authored-by: Noah Stride <noah.stride@goteleport.com>

* Address review feedback

* Fix go.mod issue breaking linter, remove duped postgres library

* Fix broken links

* Rephrase the final sentence a bit.

* Update docs/pages/machine-id/guides.mdx

Co-authored-by: Paul Gottschling <paul.gottschling@goteleport.com>

* First batch of review feedback

* Apply more suggestions from review

* Restore original go.sum / go.mod

* Show different version requirement text for cloud vs OSS/enterprise

* Add small note about `tbot db` limitations

* Add new Machine ID / Database Access diagram

* Fix lint errors

* Fix broken link

* Apply suggestions from code review

Co-authored-by: Paul Gottschling <paul.gottschling@goteleport.com>

* Address review feedback

* Fix whitespace

* Address review feedback

* Update docs/pages/machine-id/guides/databases.mdx

Co-authored-by: Paul Gottschling <paul.gottschling@goteleport.com>

Co-authored-by: Russell Jones <rjones@gravitational.com>
Co-authored-by: Noah Stride <noah.stride@goteleport.com>
Co-authored-by: Paul Gottschling <paul.gottschling@goteleport.com>
2022-07-07 23:50:31 +00:00

40 lines
815 B
Go

// This example program demonstrates how to connect to a Postgres database
// using certificates issued by Teleport Machine ID.
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/jackc/pgx/v4/stdlib"
)
func main() {
// Open connection to database.
db, err := sql.Open("pgx", fmt.Sprint(
"host=localhost ",
"port=1234 ",
"dbname=example ",
"user=alice ",
"sslmode=verify-full ",
"sslrootcert=/opt/machine-id/teleport-database-ca.crt ",
"sslkey=/opt/machine-id/key ",
"sslcert=/opt/machine-id/tlscert ",
))
if err != nil {
log.Fatalf("Failed to open database: %v.", err)
}
defer db.Close()
// Call "Ping" to test connectivity.
err = db.Ping()
if err != nil {
log.Fatalf("Failed to Ping database: %v.", err)
}
log.Printf("Successfully connected to PostgreSQL.")
}