Back to course

Connecting to PostgreSQL

Go (Golang) for Cloud-Native Microservices

Database Connectivity

Go uses the database/sql package to interact with SQL databases. You need a driver (like pgx) to connect.

Opening a Connection:

go db, err := sql.Open("pgx", "postgres://user:pass@localhost/db") if err != nil { log.Fatal(err) }

// Check connectivity err = db.Ping()

Remember: sql.Open doesn't actually connect; it just prepares the handler. Use Ping() to verify.