Unable to connect to database due to Permission issues (SQLSTATE 42501)
the docker container that has my postgres image for this project runs very well and i've been able to view the database, I'm having issues connecting my code to the databse right no. whenever i run the code it gives the error
$ go run .
2023/05/23 10:01:02 failed to connect to `host=localhost user=postgres database=blogs`: server error (FATAL: could not open file "global/pg_filenode.map": Permission denied (SQLSTATE 42501))
exit status 1
my db.go
package main
import (
"database/sql"
"log"
_ "github.com/jackc/pgconn"
_ "github.com/jackc/pgx/v4"
_ "github.com/jackc/pgx/v4/stdlib"
)
func OpenDB(dsn string) (*sql.DB, error){
db, err := sql.Open("pgx", dsn)
if err != nil{
return nil, err
}
err = db.Ping()
if err != nil{
return nil, err
}
return db, err
}
func (app *application) ConnectToDB() (*sql.DB, error){
connection, err := OpenDB(app.DSN)
if err != nil{
return nil, err
}
log.Println("Connected to Posty!:-")
return connection, nil
}
my main.go
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"net/http"
)
const port = 8080
type application struct{
Domain string
DSN string
DB *sql.DB
}
func main(){
// set app config
var app application
// read from command line
flag.StringVar(&app.DSN, "dsn", "host=localhost port=5432 user=postgres password=postgres dbname=blogs sslmode=disable timezone=UTC connect_timeout=15", "Postgres Connection String")
flag.Parse()
//connect to the database
conn, err := app.ConnectToDB()
if err != nil{
log.Fatal(err)
}
app.DB = conn
app.Domain = "example.com"
log.Println("Starting application on port", port)
//start a web server
err = http.ListenAndServe(fmt.Sprintf(":%d", port), app.routes())
if err != nil{
log.Fatal(err)
}
}