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)
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.
Related
I am trying to install cvat, so I am following the Installation guide
The first step run smoothly, however when I try to create a superuser with the command
docker exec -it cvat_server bash -ic 'python3 ~/manage.py createsuperuser'
After a few minutes, I get the following error:
Traceback (most recent call last):
File "/opt/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/opt/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
return func(*args, **kwargs)
File "/opt/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
return func(*args, **kwargs)
File "/opt/venv/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/venv/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection timed out
Is the server running on host "cvat_db" (172.18.0.3) and accepting
TCP/IP connections on port 5432?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/django/manage.py", line 21, in <module>
execute_from_command_line(sys.argv)
File "/opt/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/opt/venv/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/venv/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/venv/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 79, in execute
return super().execute(*args, **options)
File "/opt/venv/lib/python3.8/site-packages/django/core/management/base.py", line 397, in execute
self.check_migrations()
File "/opt/venv/lib/python3.8/site-packages/django/core/management/base.py", line 486, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/opt/venv/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/opt/venv/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/opt/venv/lib/python3.8/site-packages/django/db/migrations/loader.py", line 220, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/opt/venv/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/opt/venv/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/opt/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
return func(*args, **kwargs)
File "/opt/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/opt/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
self.ensure_connection()
File "/opt/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
return func(*args, **kwargs)
File "/opt/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/opt/venv/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/opt/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/opt/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
return func(*args, **kwargs)
File "/opt/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/venv/lib/python3.8/site-packages/django/utils/asyncio.py", line 33, in inner
return func(*args, **kwargs)
File "/opt/venv/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/venv/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection timed out
Is the server running on host "cvat_db" (172.18.0.3) and accepting
TCP/IP connections on port 5432?
What should I do to fix this and use CVAT?
My Ubuntu version: Ubuntu 22.04.1 LTS
I thought adding Django's authentication system might solve it so I did pip install django-auth but it changed nothing
I have had the same problem but I found a solution. Some user suggested a problem with docker installation here. My way to solve it is:
Remove docker installation completely. This can help.
Install docker from the atp repository:
sudo apt install containerd
sudo apt install docker.io
Do everything as in the CVAT installation guide, but ignore the part about installing Docker and use the commands above. With this solution some comands indicated in the installation guide will require sudo. In the guide:
Instead of docker compose up -d use sudo docker-compose up -d
Run with sudo docker-compose up
I am a newbie developing a Django site in a pipenv virtual environment. The server has been starting and working with no problems. After my computer froze and I restarted with a hard reboot, I can't get the Django server to work.
Here is the error I see after running, python manage.py runserver
((site1) ) userone#theusers-MBP site1 % python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 973, in _bootstrap_inner
self.run()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/threading.py", line 910, in run
self._target(*self._args, **self._kwargs)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/autoreload.py", line 53, in wrapper
fn(*args, **kwargs)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check_migrations()
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/core/management/base.py", line 459, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/migrations/loader.py", line 216, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django_tenants/postgresql_backend/base.py", line 122, in _cursor
cursor = super()._cursor()
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 235, in _cursor
self.ensure_connection()
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "/Users/userone/.local/share/virtualenvs/site1-wGphEfbP/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: connection to server at "localhost" (::1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?
Seems that after reboot you do not have postgres running. Can you try logging into pgadmin? Usually it gets installed with postgres default settings.
So go to your windows -start menu - programs - pgadmin.
See if you can connect from there so we can make sure that you have Postgres running before investigating further. Let me know how it goes.
First confirm that the default port for postgres is mentioned as 5432 in the settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydatabase',
'USER': 'root',
'PASSWORD': '123456',
'HOST': 'localhost',
'PORT': '5432',
}
}
Next, start the Postgres by calling:
# Linux
sudo systemctl start PostgreSQL
# Macos
brew services start postgresql
My RDS and web container are not connected.
But I did all the database-related settings in Django's settings, and I also set up AWS RDS properly.
What should I do more?
This is DATABASES of settings file of Django.
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": env("SQL_DATABASE"),
"USER": env("SQL_USER"),
"PASSWORD": env("SQL_PASSWORD"),
"HOST": env("SQL_HOST"),
"PORT": env("SQL_PORT"),
}
}
I skipped the docker-compose.yml with nginx-proxy or TLS.
When I tested in local, I made and mounted DB containers on docker-compose, but in prod environments, I didn't make DB containers because I use AWS RDS.
Will this be a problem?
Please help me.
(ps.All of PROJECT_NAME replaced the actual project name.)
This is my docker-compose.yml
version: '3.8'
services:
web:
build:
context: .
dockerfile: prod.Dockerfile
image: project:web
command: gunicorn PROJECT_NAME.wsgi:application --bind 0.0.0.0:8000
env_file:
- envs/.env.prod
volumes:
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
expose:
- 8000
entrypoint:
- sh
- config/docker/entrypoint.prod.sh
volumes:
static_volume:
media_volume:
This is what I've got error from docker
Waiting for postgres...
PostgreSQL started
Traceback (most recent call last):
File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: FATAL: database "db-PROJECT_NAME-ec2" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/usr/local/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/usr/local/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/usr/local/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
self.ensure_connection()
File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/usr/local/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: FATAL: database "db-PROJECT_NAME-ec2" does not exist
[2021-09-18 15:09:21 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2021-09-18 15:09:21 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2021-09-18 15:09:21 +0000] [1] [INFO] Using worker: sync
[2021-09-18 15:09:21 +0000] [12] [INFO] Booting worker with pid: 12
Highly likely the reason is that the AWS RDS DB instance name differs from PostgreSQL DB name.
As per AWS RDS manual
For Databases, choose the name of the new DB instance.
On the RDS console, the details for the new DB instance appear. The DB instance has a status of creating until the DB instance is created and ready for use. When the state changes to available, you can connect to the DB instance. Depending on the DB instance class and storage allocated, it can take several minutes for the new instance to be available.
As per PostgreSQL: Documentation
dbname
Specifies the name of the database to be created. The name must be unique among all PostgreSQL databases in this cluster. The default is to create a database with the same name as the current system user.
Default dbname is postgres
So, you may want to try postgres instead db-PROJECT_NAME-ec2 as a DB name.
I know it may be a novice problem but I got stuck here for quite some time.
In my azure account I wanted to deploy my django-postgresql project in a subdomain. so I did the following steps,
Created a python webapp with linux resource
Created a postgresql server (single server) with the same resource
Setup CI/CD in Development center
Also set the production.py file in my project and set those variable values in the Configuration of my App service.
I successfully created my database in postgresql using azure CLI (This is the only step I use CLI in this process)
Then I opened my SSH session and activated antenv. After that I ran the python manage.py migrate command and got the following error....
Traceback (most recent call last):
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/python/3.8.6/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: 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 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 22, in <module>
main()
File "manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
self.execute(*args, **cmd_options)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
output = self.handle(*args, **options)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
res = handle_func(*args, **kwargs)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
self.build_graph()
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
if self.has_table():
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
with self.connection.cursor() as cursor:
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
return self._cursor()
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
self.ensure_connection()
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
self.connect()
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
self.connection = self.get_new_connection(conn_params)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
return func(*args, **kwargs)
File "/opt/python/3.8.6/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 187, in get_new_connection
connection = Database.connect(**conn_params)
File "/opt/python/3.8.6/lib/python3.8/site-packages/psycopg2/__init__.py", line 127, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: 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 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
FYI, I am not using docker and azure CLI for creating webapp. I only used azure CLI to create the database.
Can you please help me with this?
Thanks.
As the error message, your app service could not connect to server, here are something you need to check:
Check if you already set all of the required app settings like mine:
Check if you enable the Allow access to Azure Service in Azure Database for PostgreSQL server's Firewall rules:
If you are new to this, you could follow this tutorial first.
It is related to the settings files for multiple deployments. Most probably one of your deployment settings uses local postgresql another one uses azure postgresql. Then because of wrong import or another reason, azure webapp uses host of local deployment (which is localhost if not provided).
I have a Django-Celery-PostgreSQL project that should partially run in Docker. Having Redis and PostgreSQL servers running locally on the machine and using virtual environment for the project everything runs smoothly. But when I try to setup Docker instance Celery seems unable to connect to the Postgres database while Django can.
When running project in Docker I put only Django and Celery in it and set network_mode: "host". Redis and Postgres remains on the local machine. Django server itself works flawlessly reading and writing data into the Postgres database, but when I try to run Celery tasks - I get following exception (thrown by django_celery_results):
[2019-08-13 11:26:24,815: ERROR/MainProcess] Pool callback raised exception: OperationalError('could not connect to server: No such file or directory\n\tIs the server running locally and accepting\n\tconnections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?\n',)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
self.connect()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/billiard/pool.py", line 1750, in safe_apply_callback
fun(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/celery/worker/request.py", line 371, in on_failure
store_result=self.store_errors,
File "/usr/local/lib/python3.6/site-packages/celery/backends/base.py", line 160, in mark_as_failure
traceback=traceback, request=request)
File "/usr/local/lib/python3.6/site-packages/celery/backends/base.py", line 342, in store_result
request=request, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django_celery_results/backends/database.py", line 38, in _store_result
using=using,
File "/usr/local/lib/python3.6/site-packages/django_celery_results/managers.py", line 50, in _inner
return fun(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django_celery_results/managers.py", line 129, in store_result
defaults=fields)
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 538, in get_or_create
return self.get(**kwargs), False
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 402, in get
num = len(clone)
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 256, in __len__
self._fetch_all()
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.6/site-packages/django/db/models/query.py", line 55, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1098, in execute_sql
cursor = self.connection.cursor()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 256, in cursor
return self._cursor()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 233, in _cursor
self.ensure_connection()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
self.connect()
File "/usr/local/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
self.connect()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 195, in connect
self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 178, in get_new_connection
connection = Database.connect(**conn_params)
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Docker-compose:
version: '3'
services:
road_data_service:
build:
context: ./
dockerfile: ./_docker/backend/local_dev/Dockerfile
volumes:
- ./:/server
network_mode: "host"
ports:
- "8326:8000"
Dockerfile:
RUN apt-get update \
&& apt-get install -y binutils libproj-dev gdal-bin \
&& rm -rf /var/lib/apt/lists/*
WORKDIR ./
COPY ./ /server
COPY _docker/backend/local_dev/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
WORKDIR /server/
CMD python3 manage.py runserver 0.0.0.0:8000 && celery -A worker_name \
worker -l info
Turned out I messed up with the DJANGO_SETTINGS_MODULE in celery.py - Celery got common settings file and not the one intended for the local development.