Go PostgreSQL Connection Example using pq client
This example shows you how to connect to PostgreSQL using lib/pq client in Go programming language. Two scripts are provided, one with sslmode=verify-full
and one without.
Prerequisites
Ensure you have the following installed on your machine:
- Go (You can install Go from here)
Usage
- Clone this repository:
git clone https://github.com/danieltprice/postgresql-connection-examples.git
- Navigate to the directory:
cd postgresql-connection-examples/go/pq
- Create a mod file
go mod init example/gopostgressql
- Get the
pq
package:
go get github.com/lib/pq
- Run the program:
go run main.go
or
go run main-ssl.go
This will print the PostgreSQL version and the current timestamp.
Always remember to handle database credentials securely in your applications. In a real-world scenario, you should not hardcode your credentials in the code. Instead, consider using environment variables or secure configuration files to handle such sensitive data.