S
S
Soul12021-10-14 14:28:06
PostgreSQL
Soul1, 2021-10-14 14:28:06

How to see PostgreSQL password?

I started to study working with PostgreSQL through Python, I found this code:

spoiler
import psycopg2
from psycopg2 import OperationalError
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT


def create_connection(db_name, db_user, db_password, db_host, db_port):
    connection = None
    try:
        connection = psycopg2.connect(
            database=db_name,
            user=db_user,
            password=db_password,
            host=db_host,
            port=db_port,
        )
        print("Connection to PostgreSQL DB successful")
    except OperationalError as e:
        print(f"The error '{e}' occurred")


connection = create_connection("postgres", "postgres", "postgres", "127.0.0.1", "5432")

When launched, it gives an error:
The error 'FATAL: password authentication failed for user "postgres"
' occurred
Googled that when installing Postgres, a password is issued, but I did not install Postgres, it was already on the computer, maybe it was installed with Windows, I don’t know. How can I view this password or create a new one? Windows 10 operating system.

And another error with from psycopg2 import OperationalError, it simply does not find OperationalError.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
galaxy, 2021-10-14
@Soul1


And another error with from psycopg2 import OperationalError, it simply does not find OperationalError

finds:
>>> from psycopg2 import OperationalError
>>> OperationalError
<class 'psycopg2.OperationalError'>
>>> psycopg2.__version__
'2.8.6 (dt dec pq3 ext lo64)'

what is the version of psycopg2?
The error 'FATAL: password authentication failed for user "postgres"

Check your pg_hba.conf . It needs an entry for 127.0.0.1 or localhost
UPD:
How can I view this password or create a new one?

The installer may set some default password, or may not set it at all (then the postgres user will not be able to log in with a password). You need to either configure pg_hba.conf to log in without a password (trust method), or log in via the console / pgadmin and set a password:
ALTER ROLE postgres WITH PASSWORD '123';or in psql Log in \password
via the console / pgadmin without a password will work only if this is configured in pg_hba.conf does, if not, change the file manually and don't forget to restart postgres).

S
Sergey Gornostaev, 2021-10-14
@sergey-gornostaev

You need to create a new user and database, assign the user as the owner of this database, and set permission in pg_hba.conf for this user to connect to the database at 127.0.0.1. You don't have to mess around with things like that.

D
Drno, 2021-10-14
@Drno

What does it mean - Postgress was ?? How could he install himself???
Do you even know how the PC and the system works? If not, you need to start by studying this ...
Ask the password from the person who installed postgress

A
anikavoi, 2021-10-15
@anikavoi

Look - nothing.
Change - galaxy replied.
Stop postgris, edit pg_hba.conf (essentially keeping the original one)
local all postgres trust
host all postgres 0.0.0.0/0 trust
host all all 127.0.0.0/8 trust
start postgris, then
calmly go to it at least psql with anything, and change the password
ALTER ROLE postgres WITH PASSWORD '123'; or in psql \password

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question