Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic: pq: password authentication failed for user "bond" #28

Open
Kai-Perets opened this issue Dec 8, 2019 · 3 comments
Open

panic: pq: password authentication failed for user "bond" #28

Kai-Perets opened this issue Dec 8, 2019 · 3 comments

Comments

@Kai-Perets
Copy link

I keep getting the following error: panic: pq: password authentication failed for user "bond"

I cannot find what's wrong with the code, as it's the one that was used in the course, and in the course it ran perfectly.
Can you find the problem?

GO CODE:

package main

import (
"database/sql"
"fmt"

_ "github.com/lib/pq"

)

func main() {
db, err := sql.Open("postgres", "postgres://bond:password@localhost/bookstore?sslmode=disable")
if err != nil {
panic(err)
}
defer db.Close()

err = db.Ping() //"Ping" checks if a connection to a database is still live, and returns an error if it isnt (so if theres no error it means its connected).
if err != nil {
	panic(err)
}
fmt.Println("You've connected to your database")

}

postgreSQL CODE:

postgres=# CREATE DATABASE bookstore;
CREATE DATABASE
postgres=# CREATE USER bond WITH PASSWORD 'password';
CREATE ROLE
postgres=# GRANT ALL PRIVILEGES ON DATABASE bookstore to bond;
GRANT

@MrMegaMango
Copy link

same issue

@Aeronio
Copy link

Aeronio commented Jan 22, 2021

Had the same issue! I have not tested it in further lectures, but I was able to connect following this article: https://www.calhoun.io/connecting-to-a-postgresql-database-with-gos-database-sql-package/

My code ended up looking something like this:

package main

import (
	"database/sql"
	"fmt"

	_ "github.com/lib/pq"
)

const (
	host     = "localhost"
	port     = 5432
	user     = "bond"
	password = "password"
	dbname   = "bookstore"
)

func main() {

	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("You connected to your database.")
}

@chrisdavies
Copy link

In my case, I had misconfigured my local postgres server. Here's the fix:

Find the pg_hba.conf location in psql:

SHOW hba_file;

Edit it...

sudo -u postgres vim /var/lib/pgsql/data/pg_hba.conf

Make sure local connections are set to password:

# IPv4 local connections:
host    all             all             127.0.0.1/32            password
# IPv6 local connections:
host    all             all             ::1/128                 password

Restart postgres:

sudo systemctl restart postgresql.service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants