CLI keeps trying to connect to local db
rob8624
HOBBYOP

3 months ago

Hi folks, as per title, I'm struggling to find out why I cannot connect to my staging environments db locally via the CLI. This maybe a codebase error on my side, but looking for help.

I link my backend (Django) service all OK, which is connected to a Postgres. But when i run migrations (via railway run.....) the Traceback is giving me [Errno 11001] getaddrinfo failed.

My settings/envs are as follows. I have checked services variable with railway varibles and my DATABASE_URL is seen. Locally, i'm using docker, everything other than this works, it's deployed etc etc, but i just cannot use the railway CLI.

`if DATABASE_URL:

DATABASES = {
    'default': dj_database_url.config(default=DATABASE_URL)
}

else:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.getenv('POSTGRES_DATABASE'),
        'USER': os.getenv('POSTGRES_USER'),
        'PASSWORD': os.getenv('POSTGRES_PASSWORD'),
        'HOST': os.getenv('POSTGRES_HOST'),
        'PORT': os.getenv('POSTGRES_PORT'),
    }
}

`

PROJECT ID : 4f1d4e07-9f0f-46dd-b818-1e47b72f7c64

Solved

3 Replies

rob8624
HOBBYOP

3 months ago

Do i need to use my DATABASE_PUBLIC_URL when connecting to my databse from the CLI. At the moment it's using the internal adderess given to DATABASE_URL?


rob8624
HOBBYOP

3 months ago

Doesn't work, even using the public db

Tried this, same error. The print to debug is giving me the publice db address.

`RAILWAY_CLI = True

def get_database_url(database_cli):

if database_cli:

return os.getenv('DATABASE_PUBLIC_URL')

else:

return os.getenv('DATABASE_URL')

DATABASE_URL = get_database_url(RAILWAY_CLI)

print(f"DEBUG DATABASE_URL: {DATABASE_URL}")

if DATABASE_URL:

DATABASES = {
    'default': dj_database_url.config(default=DATABASE_URL)
}

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.getenv('POSTGRES_DATABASE'),
        'USER': os.getenv('POSTGRES_USER'),
        'PASSWORD': os.getenv('POSTGRES_PASSWORD'),
        'HOST': os.getenv('POSTGRES_HOST'),
        'PORT': os.getenv('POSTGRES_PORT'),
    }
}`

rob8624
HOBBYOP

3 months ago

Ok. I managed to connect i had to use parse() on dj_database_url as config() will read from envs always.


Status changed to Solved passos 3 months ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...