Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using PostgreSQL with Go

Description

In this tutorial, we will explore how to use PostgreSQL with Go, covering setup, connecting to a PostgreSQL database, and performing basic operations using the database/sql package and lib/pq driver.

Setup

Before starting, ensure you have Go installed on your system. Install the lib/pq package, which provides a PostgreSQL driver for Go:

go get github.com/lib/pq

Connecting to PostgreSQL

To connect to a PostgreSQL database in Go, use the following code snippet:

package main import ( "database/sql" "fmt" _ "github.com/lib/pq" ) func main() { // Replace with your PostgreSQL connection details connStr := "host=localhost dbname=mydatabase user=myuser password=mypassword sslmode=disable" db, err := sql.Open("postgres", connStr) if err != nil { panic(err) } defer db.Close() err = db.Ping() if err != nil { panic(err) } fmt.Println("Connected to PostgreSQL database") }
Connected to PostgreSQL database

Performing Basic Operations

Here are examples of basic CRUD operations using Go with PostgreSQL:

Create (INSERT)

package main import ( "database/sql" "fmt" _ "github.com/lib/pq" ) func main() { // Replace with your PostgreSQL connection details connStr := "host=localhost dbname=mydatabase user=myuser password=mypassword sslmode=disable" db, err := sql.Open("postgres", connStr) if err != nil { panic(err) } defer db.Close() // Execute INSERT statement _, err = db.Exec("INSERT INTO employees (name, age) VALUES ($1, $2)", "Alice", 30) if err != nil { panic(err) } fmt.Println("Record inserted successfully.") }
Record inserted successfully.

Read (SELECT)

package main import ( "database/sql" "fmt" _ "github.com/lib/pq" ) func main() { // Replace with your PostgreSQL connection details connStr := "host=localhost dbname=mydatabase user=myuser password=mypassword sslmode=disable" db, err := sql.Open("postgres", connStr) if err != nil { panic(err) } defer db.Close() // Execute SELECT statement rows, err := db.Query("SELECT name, age FROM employees WHERE age > $1", 25) if err != nil { panic(err) } defer rows.Close() for rows.Next() { var name string var age int err := rows.Scan(&name, &age) if err != nil { panic(err) } fmt.Printf("Name: %s, Age: %d\n", name, age) } }
Name: Alice, Age: 30

Update

package main import ( "database/sql" "fmt" _ "github.com/lib/pq" ) func main() { // Replace with your PostgreSQL connection details connStr := "host=localhost dbname=mydatabase user=myuser password=mypassword sslmode=disable" db, err := sql.Open("postgres", connStr) if err != nil { panic(err) } defer db.Close() // Execute UPDATE statement _, err = db.Exec("UPDATE employees SET age = $1 WHERE name = $2", 31, "Alice") if err != nil { panic(err) } fmt.Println("Record updated successfully.") }
Record updated successfully.

Delete

package main import ( "database/sql" "fmt" _ "github.com/lib/pq" ) func main() { // Replace with your PostgreSQL connection details connStr := "host=localhost dbname=mydatabase user=myuser password=mypassword sslmode=disable" db, err := sql.Open("postgres", connStr) if err != nil { panic(err) } defer db.Close() // Execute DELETE statement _, err = db.Exec("DELETE FROM employees WHERE name = $1", "Alice") if err != nil { panic(err) } fmt.Println("Record deleted successfully.") }
Record deleted successfully.

Conclusion

Explanation: This concludes the tutorial on using PostgreSQL with Go. You've learned how to install the necessary dependencies, connect to a PostgreSQL database, and perform basic CRUD operations.