Created
August 18, 2016 13:56
-
-
Save eikaas/87164be732b96743c24776111081e839 to your computer and use it in GitHub Desktop.
connect to postgres (bmizerany/pq)
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
package main | |
import ( | |
"database/sql" | |
"log" | |
_ "github.com/bmizerany/pq" | |
) | |
/* Database and User creation in psql | |
CREATE DATABASE testdb; | |
CREATE USER testuser WITH PASSWORD 'testpw'; | |
GRANT ALL PRIVILEGES ON DATABASE testdb TO testuser; | |
*/ | |
func main() { | |
db, err := sql.Open("postgres", "dbname=testdb user=testuser password=testpw port=5432 sslmode=disable") | |
if err != nil { | |
log.Panic(err) | |
} | |
defer db.Close() | |
err = db.Ping() | |
if err != nil { | |
log.Panic(err) | |
} | |
log.Println("db.Ping() did not return an error, meaning database connection was established.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment