Cant connect to Railway postgress db with Django

danielmohammadiiTRIAL

8 months ago

Hey,

I’m following this YouTube video (https://www.youtube.com/watch?v=AjKhxWgGpjY&t=332s) to connect to the database, but I'm getting this error. I would really appreciate the help.

eorror

Traceback (most recent call last):

File "C:\project\Django\highlighara\manage.py", line 22, in <module>

main()

File "C:\project\Django\highlighara\manage.py", line 18, in main

execute_from_command_line(sys.argv)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\core\management\__init__.py", line 442, in execute_from_command_line

utility.execute()

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\core\management\__init__.py", line 436, in execute

self.fetch_command(subcommand).run_from_argv(self.argv)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\core\management\base.py", line 413, in run_from_argv

self.execute(*args, **cmd_options)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\core\management\base.py", line 459, in execute

output = self.handle(*args, **options)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\core\management\base.py", line 107, in wrapper

res = handle_func(*args, **kwargs)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\core\management\commands\migrate.py", line 118, in handle

executor = MigrationExecutor(connection, self.migration_progress_callback)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\migrations\executor.py", line 18, in init

self.loader = MigrationLoader(self.connection)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\migrations\loader.py", line 58, in init

self.build_graph()

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\migrations\loader.py", line 235, in build_graph

self.applied_migrations = recorder.applied_migrations()

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\migrations\recorder.py", line 89, in applied_migrations

if self.has_table():

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\migrations\recorder.py", line 63, in has_table

with self.connection.cursor() as cursor:

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\utils\asyncio.py", line 26, in inner

return func(*args, **kwargs)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\backends\base\base.py", line 320, in cursor

return self._cursor()

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\backends\base\base.py", line 296, in _cursor

self.ensure_connection()

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\utils\asyncio.py", line 26, in inner

return func(*args, **kwargs)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\backends\base\base.py", line 278, in ensure_connection

with self.wrap_database_errors:

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\utils.py", line 91, in exit

raise dj_exc_value.with_traceback(traceback) from exc_value

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\backends\base\base.py", line 279, in ensure_connection

self.connect()

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\utils\asyncio.py", line 26, in inner

return func(*args, **kwargs)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\backends\base\base.py", line 256, in connect

self.connection = self.get_new_connection(conn_params)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\utils\asyncio.py", line 26, in inner

return func(*args, **kwargs)

File "C:\project\Django\highlighara\_venv\lib\site-packages\django\db\backends\postgresql\base.py", line 332, in get_new_connection

connection = self.Database.connect(**conn_params)

File "C:\project\Django\highlighara\_venv\lib\site-packages\psycopg\connection.py", line 98, in connect

attempts = conninfo_attempts(params)

File "C:\project\Django\highlighara\_venv\lib\site-packages\psycopg\_conninfo_attempts.py", line 50, in conninfo_attempts

raise e.OperationalError(str(last_exc))

django.db.utils.OperationalError: [Errno 11001] getaddrinfo failed

my setting

my .env:

ENVIRONMENT=dev
SECRET_KEY=secretkey
DATABASE_URL= url 
ATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}


POSTGRES_LOCALLY = True
if ENVIRONMENT == "prod" or POSTGRES_LOCALLY == True:
    DATABASES["default"] = dj_database_url.parse(env("DATABASE_URL"))

5 Replies

danielmohammadiiTRIAL

8 months ago

  • DATABASES =


8 months ago

When developing locally you will need to use the public database URL.


danielmohammadiiTRIAL

8 months ago

Thank you very much , i did change but now i am getting this error, i copied my setting and .env
error:

File "C:\project\Django\highlighara\_venv\lib\site-packages\psycopg\connection.py", line 119, in connect

raise last_ex.with_traceback(None)

django.db.utils.OperationalError: connection failed: connection to server at "50.7.85.220", port 32767 failed: could not receive data from server: Socket is not connected (0x00002749/10057)

could not send SSL negotiation packet: Socket is not connected (0x00002749/10057)

my setting

POSTGRES_LOCALLY = True
if ENVIRONMENT == "prod" or POSTGRES_LOCALLY == True:
    database_config = dj_database_url.parse(env("DATABASE_URL"))
    database_config["OPTIONS"] = {
        "connect_timeout": 30,
        "keepalives": 1,
        "keepalives_idle": 30,
        "keepalives_interval": 10,
        "keepalives_count": 5,
        "tcp_user_timeout": 2000,
    }
    database_config["ATOMIC_REQUESTS"] = True
    DATABASES["default"] = database_config
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": BASE_DIR / "db.sqlite3",
    }
}
DATABASE_URL=postgresql://postgres:lIuaNTS....kTGjeLXUtVRQLdD@junction.proxy.rlwy.net:32767/railway

danielmohammadiiTRIAL

8 months ago

When developing locally you will need to use the public database URL.

I would really appreciate your help with this. Thank you very much!


8 months ago

I am able to connect to the database just fine, are you sure you aren't behind some kind of work or school firewall?