简体   繁体   中英

Connect to Postgres DB by a user which has no password

I created a user by:

sudo -u postgres psql -c "CREATE USER sample;"

Then created a database:

sudo -u postgres psql -c "CREATE DATABASE practice OWNER sample;"

Now I'm trying to connect to this DB by following snippet:

dsn := url.URL{
        User:     url.UserPassword("sample", ""),
        Scheme:   "postgres",
        Host:     fmt.Sprintf("%s", "localhost"),
        Path:     "practice",
        RawQuery: (&url.Values{"sslmode": []string{"disable"}}).Encode(),
}
db, err := gorm.Open(
        "postgres",
        dsn.String(),
)
if err != nil {
        log.Fatal(err)
}

But the output is:

2020/07/07 16:43:44 pq: password authentication failed for user "sample"

How do I connect to DB with a user which has no password?

Either assign the user a password and then use it, or change your pg_hba.conf file so that some other method of authentication is used for that user/dbname/connection-method/host (and then restart your server so the change takes effect).

sudo -u postgres psql -c "ALTER USER sample password 'yahkeixuom5R';"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM