Search code examples
djangoubuntupostgres-14

Connection error between Django and Postgres


I'm setting up an Amazon AWS test server with Django on which I use Postgres as a database. This was my way here:

$ sudo apt update
$ sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib 

I downloaded my files from Github and created a virtual environment for it and inside I installed psycopg2.

$ pip install psycopg2

How I configured Postgres:

$ sudo -i -u postgres
$ psql
$ createuser --interactive (super user);
$ createdb Filme;

Some other information that may be useful about Postgres:

$ service postgresql status

> postgresql.service - PostgreSQL RDBMS
     Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset:>
     Active: active (exited) since Wed 2022-10-05 17:08:25 UTC; 20h ago
   Main PID: 977 (code=exited, status=0/SUCCESS)
        CPU: 1ms

Oct 05 17:08:25 ip-172-31-29-151 systemd[1]: Starting PostgreSQL RDBMS...
Oct 05 17:08:25 ip-172-31-29-151 systemd[1]: Finished PostgreSQL RDBMS.


$ pg_lsclusters

> Ver Cluster Port Status Owner    Data directory              Log file
14  main    5432 online postgres /var/lib/postgresql/14/main /var/log/postgresql/postgresql-14-main.log

In my settings.py file of my project:

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

The error occurs when I try to run the migration to the new database:

$ (env) $ python3 manage.py migrate

> Traceback (most recent call last):
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
    self.connect()
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/backends/base/base.py", line 263, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 215, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  role "ubuntu" does not exist


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/ubuntu/Filmes/manage.py", line 22, in <module>
    main()
  File "/home/ubuntu/Filmes/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute
    output = self.handle(*args, **options)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/core/management/base.py", line 96, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 114, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/migrations/loader.py", line 58, in __init__
    self.build_graph()
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/migrations/loader.py", line 235, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 81, in applied_migrations
    if self.has_table():
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/migrations/recorder.py", line 57, in has_table
    with self.connection.cursor() as cursor:
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/backends/base/base.py", line 323, in cursor
    return self._cursor()
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/backends/base/base.py", line 299, in _cursor
    self.ensure_connection()
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/backends/base/base.py", line 281, in ensure_connection
    with self.wrap_database_errors:
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/backends/base/base.py", line 282, in ensure_connection
    self.connect()
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/backends/base/base.py", line 263, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/django/db/backends/postgresql/base.py", line 215, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/home/ubuntu/Filmes/env/lib/python3.10/site-packages/psycopg2/__init__.py", line 122, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  role "ubuntu" does not exist

PS: I also followed the DigitalOcean tutorial, but I end up falling into an almost identical error. (https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04)


Solution

  • You've only configured the ENGINE and NAME settings. You have left all the other DB connection settings as default, such as the username, password, etc. So it is trying to connect to the database using the username of the current Linux user, which appears to be ubuntu. I'm guessing when you ran createuser you didn't specify a username of ubuntu and you didn't leave the password blank, so you need to set those settings in the Django database config.

    Look at all those database connection settings in the Digital Ocean tutorial you linked, compared to the two settings you have configured in your settings file.


    Note that since you are just running Django and Postgres on the same server, there is nothing AWS specific about your issue. It is the same as how you would configure Django/PostgreSQL on any other Linux server. If you are searching for "django connect to postgres on AWS" or something like that, (focusing on the AWS aspect of your problem, when in actuality there is no AWS aspect to your problem at all), that is probably preventing you from finding answers to your question.