I'm trying to create a practice program where I accept API requests from a front end and then store data to a postgres database.
In the included code you can see that I define my parameters for connecting to the local database server, however when I run the Go program after starting the postgres server in my terminal, I get the error as included 'pq: database "root" does not exist'.
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
user = "root"
password = ""
dbname = "postgres"
)
func ConnectDatabase() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbname)
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Successfully connected!")
}
Image of the console containing the error
I'm quite new to this, but it doesn't really make sense to me as I'm directly specifiying which database to use (which is obviously not "root"). Could it somehow have something to do with postgres by default trying to access a database with the same name as my username. If so what is the point of specifying which databse I want to connect to?
Thanks for the help in advance.