-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added basic postgres code to main.go
- Loading branch information
1 parent
06aa658
commit c7b6efb
Showing
2 changed files
with
37 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,40 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "github.com/lib/pq" | ||
) | ||
|
||
const ( | ||
host = "127.0.0.1" | ||
port = 65318 | ||
user = "" | ||
password = "" | ||
dbname = "" | ||
) | ||
|
||
func main() { | ||
// cmd.Execute() | ||
// connection string | ||
psqlconn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname) | ||
|
||
// open database | ||
db, err := sql.Open("postgres", psqlconn) | ||
CheckError(err) | ||
|
||
// close database | ||
defer db.Close() | ||
|
||
// check db | ||
err = db.Ping() | ||
CheckError(err) | ||
|
||
fmt.Println("Connected!") | ||
} | ||
|
||
func CheckError(err error) { | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |