Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connect to a DB using psycopg2 without password

I have a postgres database on my localhost I can access without a password

$ psql -d mwt psql (8.4.12) Type "help" for help.  mwt=# SELECT * from vatid;   id   | requester_vatid |...   -----+-----------------|...      1719 | IT00766780266   |... 

I want to access that database from django. So I put in DATABASES

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.postgresql_psycopg2',         'NAME': 'mwt',         'USER': 'shaoran',         'HOST': 'localhost'     } } 

Since I don't need a password to access my test database I didn't provide any PASSWORD value in the settings.

$ ./manage.py shell >>> from polls.models import Vatid >>> Vatid.objects.all()   connection_factory=connection_factory, async=async)   OperationalError: fe_sendauth: no password supplied 

I tried using PASSWORD: '' but I get the same error message. I tried to use PASSWORD: None but that didn't help either.

I've been searching the django documentation about this but I cannot find anything useful. It is possible to configure django.db.backends.postgresql_psycopg2 to accept empty password?

like image 255
Pablo Avatar asked Jun 25 '12 15:06

Pablo


People also ask

How do I connect to a Postgres database using Python?

To establish connection with the PostgreSQL database, make sure that you have installed it properly in your system. Open the PostgreSQL shell prompt and pass details like Server, Database, username, and password. If all the details you have given are appropriate, a connection is established with PostgreSQL database.

Can psycopg2 connect to MySQL?

Both MySQL and PostgreSQL are supported. You would use psycopg2 to connect to Postgres and e.g. MySQLdb to connect to MySQL. You also have to change the connection string.

How do I connect to a PostgreSQL database?

The default username for postgres is postgres. (If you are using Advanced Server it is enterprisedb.) On a Mac or Windows, you are able to connect to the default instance by simply hitting enter at the shell or command prompt when trying to run psql and keying in the password.


1 Answers

Surprisingly, the answer is to not specify a host at all. If you do this,

DATABASES = {     'default': {         'ENGINE': 'django.db.backends.postgresql_psycopg2',         'NAME': 'mwt',     } } 

Then psycopg2 will connect using a Unix socket in the same manner as psql. When you specify a HOST, psycopg2 will connect with TCP/IP, which requires a password.

like image 189
joerick Avatar answered Sep 18 '22 14:09

joerick