2

I installed postgres with Django following this guide

Here I created a database and a user in the postgres command line :

CREATE DATABASE myProject;
CREATE USER myUser WITH PASSWORD '1234'; 

When I try to connect to this database, either through Django or directly via pscopg2 I get the wrong password error.

#connect through python
import psycopg2
psycopg2.connect("dbname=myProject user=myUser host=localhost password=1234 port=5432")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/data/project/venv/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL:  password authentication failed for user "myUser"
FATAL:  password authentication failed for user "myUser"

In this question I've replaced my actual project names, user name and password. But I've double, triple checked and the names/passwords I'm writing down are all correct.

How am I getting a bad password request? How can I fix this?

EDIT: The contents of my pg_hba.conf file:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5

2 Answers2

2

For anybody finding this question, let it be noted that apparently the database name and the username are always in lowercase, even if you create them with capitals.

As such, creating a database and user like this:

CREATE DATABASE myProject;
CREATE USER myUser WITH PASSWORD '1234';

Means the exact same as doing this:

CREATE DATABASE myproject;
CREATE USER myuser WITH PASSWORD '1234'; 
0

Try this for creating the user for Postgres

postgres=# create user username with encrypted password 'password';

Add permissions

postgres=# grant all on database db_name to username;

other useful commands might help

sudo -u postgres psql

\du+ #list of user

\l+ # list of DB

Credits to this artical

Amit
  • 1
  • 1