Created
July 25, 2021 18:02
-
-
Save chrisjsimpson/0312a6439739cab3ffcc22a398de8548 to your computer and use it in GitHub Desktop.
Go postgres connect to database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "fmt" | |
import "os" | |
import "log" | |
import ( | |
"database/sql" | |
_ "github.com/lib/pq" | |
) | |
func main() { | |
connStr := "user=postgres password=changeme dbname=database sslmode=disable" | |
db, err := sql.Open("postgres", connStr) | |
if err != nil { | |
log.Fatal(err) | |
} | |
rows, err := db.Query("SELECT NOW()") | |
if err != nil{ | |
fmt.Println("pq error:", err) | |
os.Exit(1) | |
} | |
_ = rows | |
fmt.Println("Connect OK") | |
os.Exit(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment