1

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)
    }

}
jarlh
  • 42,561
  • 8
  • 45
  • 63
Kodeforce
  • 39
  • 6
  • 2
    Provide more context about your problem. The command you used for running your container, docker-compose.yml(if you have one), show us some code your are trying to connect with. That will help us to see and identify the exact problem. Also See [How to ask](https://stackoverflow.com/help/how-to-ask) – PRATHEESH PC May 23 '23 at 09:27
  • Hope this helps, https://stackoverflow.com/questions/7445935/postgres-is-failing-with-could-not-open-relation-mapping-file-global-pg-fileno – PRATHEESH PC May 23 '23 at 10:05
  • hi @PRATHEESHPC, thanks for taking a look, I'm having problems connecting both issues as the one you're referencing has Postgres installed locally and mine is being pulled from Docker – Kodeforce May 23 '23 at 10:09

1 Answers1

0

global/pg_filenode.map is a file in the filesystem, which the database process owner needs to have access to in order for the database to work. If it doesn't have access to this file, then your database is corrupted. I don't know how that could have happened, presumably something outside the database has mucked around with things it shouldn't have. Fixing this could be as easy as changing the owner/priv of this file back to what it should be, or this could be a symptom of some much deeper issue.

I would argue that this it is a bug that this error is reported as 42501. That code should be used for errors occurring when the database fails its internal permission system, which this is not. This case should be reported as an error starting with either 58 or XX.

jjanes
  • 37,812
  • 5
  • 27
  • 34