Opened 6 years ago

Closed 3 years ago

#9920 closed Bug (fixed)

Empty string is not the same as typing "localhost" in DATABASE_HOST in settings.py

Reported by: crazy2k Owned by: nobody
Component: Documentation Version: 1.0
Severity: Normal Keywords: postgresql host localhost empty
Cc: alan.justino@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I was reading Django's book (http://www.djangobook.com/). At Chapter 5, you start playing a little with the database server. I chose PostgreSQL, and I filled the settings accordingly. However, when I tried:

>>> from django.db import connection
>>> cursor = connection.cursor()

(after running python manage.py shell)
I got an error.

I had left the DATABASE_HOST setting blank in settings.py, because it says:
DATABASE_HOST = # Set to empty string for localhost. Not used with sqlite3.

I made it work by explictly adding "localhost" there.

The version of Django I've installed is 1.0-1ubuntu1.

Attachments (1)

django-9920.diff (841 bytes) - added by chromano 4 years ago.
Comment improvement

Download all attachments as: .zip

Change History (16)

comment:1 follow-up: Changed 6 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

(after running python manage.py shell) I got an error.

Please, tell us what the error was; better yet, paste the traceback.

It could be that Postgres is configured to accept conections with the user you are using via TCP on localchost and not via Unix sockets (empty string).

comment:2 Changed 6 years ago by ubernostrum

  • Resolution set to invalid
  • Status changed from new to closed

This is almost certainly a case of the server expecting a different type of connection; closing invalid because it a) works fine for me and b) references a third-party book (documentation bugs filed here should only reference things listed in pages on docs.djangoproject.com, not on other sites, regardless of who wrote the stuff you found on another site).

comment:3 in reply to: ↑ 1 Changed 6 years ago by crazy2k

Replying to ramiro:

(after running python manage.py shell) I got an error.

Please, tell us what the error was; better yet, paste the traceback.

It could be that Postgres is configured to accept conections with the user you are using via TCP on localchost and not via Unix sockets (empty string).

Sorry. Here it is:

Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.db import connection
>>> cursor = connection.cursor()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/var/lib/python-support/python2.5/django/db/backends/__init__.py", line 56, in cursor
    cursor = self._cursor(settings)
  File "/var/lib/python-support/python2.5/django/db/backends/postgresql_psycopg2/base.py", line 84, in _cursor
    self.connection = Database.connect(conn_string, **self.options)
OperationalError: FATAL:  Ident authentication failed for user "django-user"

Replying to ubernostrum:

This is almost certainly a case of the server expecting a different type of connection; closing invalid because it a) works fine for me and b) references a third-party book (documentation bugs filed here should only reference things listed in pages on docs.djangoproject.com, not on other sites, regardless of who wrote the stuff you found on another site).

a) sounds partially reasonable, but b) has no importance. That comment in settings.py is written by Django, not by some third-party book's author.

comment:4 Changed 6 years ago by moberley

This is an issue with PostgreSQL configuration not Django. However, I also encountered this problem and I found this ticket when I was trying to figure it out. Therefore, I'm posting what I found out for anyone else who ends up here. (Incidentally, I'm also using the Ubuntu package 1.0-1ubuntu1.)

As I understand it, the blank DATABASE_HOST setting uses a unix-domain socket connection, which is also known as a "local" connection in PostgreSQL's pg_hba.conf file. On the other hand, actually specifying 'localhost' for DATABASE_HOST uses a TCP/IP socket, i.e. a "host" connection in pg_hba.conf.

By default, at least on my computer, PostgreSQL is configured to use ident authentication (with the "sameuser" map) for unix-domain socket (local) connections and md5 password authentication for TCP/IP (host) connections from localhost. The latter (ident sameuser) method only works if PostgreSQL database user is the same as the operating system user the connection runs as. In most cases, the PostgresQL database user will be different that the operating system user.

Specifying 'localhost' explicitly works when an empty string doesn't because the former uses a TCP/IP socket and therefore md5 password authentication which will work as long as your database roles are set up correctly.

It's possible to create a usermap so that the ident authentication works, but I found it easier just to enable md5 password authentication for the Django database user. Edit the pg_hba.conf file (on my computer /var/etc/postgresql/8.3/main/pg_hba.conf) by adding the following line (replacing "django-user" with whatever your Django database username is).

local all django-user md5

Put that line after the "TYPE DATABASE USER CIDR-ADDRESS METHOD" heading line but before the default "local all all ident sameuser" line.

This worked for me.

comment:5 Changed 6 years ago by neomilium

  • Resolution invalid deleted
  • Status changed from closed to reopened

According to moberley and the fact that empty string results a socket connection, instead of localhost connection, comment in "settings.py" should be "# Set to empty string for socket connection." but not "# Set to empty string for localhost.".

comment:6 Changed 6 years ago by mtredinnick

That change would only confuse things further. Network connections use sockets as well. The current comment is correct. It doesn't say it is the same connection method as putting "localhost" in there. It does say that it will connect to the server on the local host.

A patch to the documentation (databases.txt probably) to point to the right place in the online documentation for each database we support (MySQL and PostgreSQL, in particular) for connection methods would be appropriate. A change to the very short reminder comment in settings.py isn't, since, as your attempt shows, it isn't clearer, nor more understandable.

comment:7 follow-up: Changed 6 years ago by jacob

  • Resolution set to wontfix
  • Status changed from reopened to closed

Marking wontfix per moberley and Malcolm.

comment:8 in reply to: ↑ 7 Changed 5 years ago by alanjds

  • Cc alan.justino@… added
  • Resolution wontfix deleted
  • Status changed from closed to reopened

Replying to jacob:

Marking wontfix per moberley and Malcolm.

Sorry by reopening what a commiter closed, but 'localhost' is for me a reminder of network connection to 127.0.0.1

The comment as now with 'localhost' suggests me that something like this occours behind the scenes:

if DATABASE_HOST == '':
    DATABASE_HOST = 'localhost'

who is simple not true.

What about changing from "Set to empty string for localhost." to "Set to empty string for local host (via file socket)." ?

At least, I ask you to allow a change from 'localhost' to 'local host'.

comment:9 Changed 5 years ago by russellm

  • Component changed from Uncategorized to Documentation
  • Triage Stage changed from Unreviewed to Accepted

Marking accepted for the documentation fix that Malcolm suggested.

comment:10 Changed 4 years ago by SmileyChris

  • Severity set to Normal
  • Type set to Bug

comment:11 Changed 4 years ago by anonymous

  • Easy pickings unset
  • UI/UX unset

I installed fresh recently:

user@userbook:~/Projects/stated$ python -c "import django; print django.VERSION" 
(1, 3, 0, 'final', 0)

This error is still out there. I almost went mad trying to get postgres working. Until I thought 'maybe the docs are lying'.

comment:12 Changed 4 years ago by aaugustin

#16685 was a duplicate.

Changed 4 years ago by chromano

Comment improvement

comment:13 Changed 3 years ago by timo

  • Has patch set

comment:14 Changed 3 years ago by claudep

LGTM about content. As for formatting, what about adding a line below NAME stating that "The following settings are not used with sqlite3"?

comment:15 Changed 3 years ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from reopened to closed

In [f7142b6111fef01c2b1a4a37219c3dfbfdea2dc2]:

Fixed #9920 - Clarfied empty string vs. localhost in settings database host.

Thanks chromano for the draft patch.

Note: See TracTickets for help on using tickets.
Back to Top