0

I'm new on Postgresql, and here I'm trying to make a connection between Python3 and Postgresql. Now I'm trying to create a new database on post 8888 on localhost. Here's my code from Python 3, and also when I try to debug this code, I do get a error, but I think that's because I didn't launch any server, on Postgresql. On the other side, I can't create a database on SQL, the server does say :

Server doesn't listen The server doesn't accept connections: the connection library reports could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 8888?

import psycopg2
#Connect to db

con = psycopg2.connect(
    host = "localhost",
    database = "Vartotojai",
    user = "postgres",
    password = "postgres"
)

cur = con.cursor()
# execute
cur.execute("select id, name from useriai")

rows = cur.fetchall() 

for r in rows:
    print(f"id {r[0]} name {r[1]}")


cur.close()

# Close connection
con.close()

Exception has occurred: psycopg2.OperationalError FATAL: password authentication failed for user "postgres" FATAL: password authentication failed for user "postgres"

J.Macro
  • 3
  • 3
  • You don't specify a port in your `connect` arguments. That means that connect goes to the PostgreSQL default port 5432. Apparently, a server is listening there, because it refuses your connection. Why do you expect it to listen on port 8888? – shmee Feb 21 '19 at 13:42
  • After chaning the port to 5432, I do get this error 15:52:35: Error: Error connecting to the server: fe_sendauth: no password supplied – J.Macro Feb 21 '19 at 13:54
  • I tried to connect with password and user - postgres and I still get error.An error has occurred: 15:54:41: Error: Error connecting to the server: FATAL: password authentication failed for user "postgres" FATAL: password authentication failed for user "postgres" – J.Macro Feb 21 '19 at 13:54
  • Yes, because, based on the platform and Postgres version, the user `postgres` might not have a password set and/or its auth method might be `peer` instead of `md5`. Please see your server's docs on the [pg_hba.conf](https://www.postgresql.org/docs/11/auth-pg-hba-conf.html). If you're on Windows, you might want to check [here](https://stackoverflow.com/questions/18698889/postgresql-windows-is-there-a-default-password) – shmee Feb 21 '19 at 14:04
  • How could I access the pg_hba.conf ? – J.Macro Feb 21 '19 at 14:05
  • I don't know. You haven't told us the OS you are on. Have you tried googling `pg_hba.conf location` by chance? – shmee Feb 21 '19 at 14:13
  • I'm on the linux ubuntu 18 – J.Macro Feb 21 '19 at 14:31
  • /etc/postgresql/10/main/pg_hba.conf is my cfg location – J.Macro Feb 21 '19 at 14:40

0 Answers0