Skip to content Skip to sidebar Skip to footer

Postgresql & Psycopg2: Database Does Not Exist

I am trying to establish a connection to a database like this: psycopg2.connect(database='movies', user='lfcj', host='127.0.0.1'); my pg_hba.conf file has a line: TYPE __ DATABAS

Solution 1:

Your connection string should look like this:

psycopg2.connect("dbname=test user=postgres password=secret")

The connect method will give you a connection to the database so you will probably need to store it in a variable. Typically like this:

conn = psycopg2.connect("dbname=test user=postgres password=secret")

You can also use a set of keywords like this:

conn = psycopg2.connect(database="test", user="postgres", password="secret")

Solution 2:

Could you try connecting to the "postgres" database:

psql -d postgres

and then execute the following command:

show data_directory;

On 9.3.1.0-alpha1 that should return

/Users/USERNAME/Library/Application Support/Postgres93/var

Then check which databases exist on the server using the shorthand \l

If all that works, it seems that Postgresapp called initdb successfully, but createdb failed. You can do that manually, just execute createdb USERNAME from the terminal (not inside psql)

Solution 3:

I had the same problem here.

I had two versions of postgres installed. One in port 5432 and the other in port 5433, as you can see here

When I tried to connect with:

conn = pg2.connect(dbname='my_db_name', user='my_user', host='localhost', port=5433, password='my_pass')

It worked and made the connection with all the databases within postgres version 9.5. When I switched to port 5432 I made the connection to the databases inside postgres version 11.

Post a Comment for "Postgresql & Psycopg2: Database Does Not Exist"