R
Railway3mo ago
Nahasco

Postgres Database private url, is it faster and how to use it?

I have a Django rest framework setup with postgress db and it seems the connection is slow. I noticed there is a database private url and i wonder if this is a faster connection. If it is how can I configure it in my project? I read the doc article about private networking but I am new here and couldnt really understand how to do it. Any help is greatly appreciated. Thanks.
Solution:
can you add a 3 second sleep to your start command
Jump to solution
27 Replies
Percy
Percy3mo ago
Project ID: 643c9962-5fa0-449c-82e0-67b30f66c552
Nahasco
Nahasco3mo ago
643c9962-5fa0-449c-82e0-67b30f66c552
Brody
Brody3mo ago
what environment variables do you currently use in your database configuration in the settings.py file?
Nahasco
Nahasco3mo ago
I previously used to parse the DATABASE URL thats in my env variables. But I tried today to specify the fields such as PG_HOST, user, password, etc.. but I received the same results. DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": os.environ["PGDATABASE"], "USER": os.environ["PGUSER"], "HOST": os.environ["PGHOST"], "PASSWORD": os.environ["PGPASSWORD"], "PORT": os.environ["PGPORT"], } }
Brody
Brody3mo ago
can you show me the code you used to parse those options from the url environment variable, if we can get that method working I think it would be the easiest
Nahasco
Nahasco3mo ago
Sure
db_url = os.environ["DATABASE_URL"]
DATABASES = {"default": dj_database_url.parse(f"{db_url}")}
db_url = os.environ["DATABASE_URL"]
DATABASES = {"default": dj_database_url.parse(f"{db_url}")}
Brody
Brody3mo ago
why not just
db_url = os.environ["DATABASE_URL"]
DATABASES = {"default": dj_database_url.parse(db_url)}
db_url = os.environ["DATABASE_URL"]
DATABASES = {"default": dj_database_url.parse(db_url)}
Nahasco
Nahasco3mo ago
Right, no idea why I did it they way I did
Brody
Brody3mo ago
and what is the DATABASE_URL set to?
Nahasco
Nahasco3mo ago
The postgres DATABASE_URL variable from postgres
Brody
Brody3mo ago
it should be set to ${{Postgres.DATABASE_PRIVATE_URL}}
Nahasco
Nahasco3mo ago
i tried that but it caused connection issues django.db.utils.OperationalError: could not translate host name "postgres-9l4v.railway.internal" to address: Name or service not known
Brody
Brody3mo ago
okay is this a nixpacks or a dockerfile deployment
Nahasco
Nahasco3mo ago
nixpacks
Solution
Brody
Brody3mo ago
can you add a 3 second sleep to your start command
Nahasco
Nahasco3mo ago
how can i do that?
Brody
Brody3mo ago
what's your current start command and where is it defined?
Nahasco
Nahasco3mo ago
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS"
},
"deploy": {
"startCommand": "python manage.py migrate && python manage.py collectstatic --noinput && gunicorn --timeout 500 tibian_backend.wsgi",
"restartPolicyType": "NEVER",
"restartPolicyMaxRetries": 10
}
}
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS"
},
"deploy": {
"startCommand": "python manage.py migrate && python manage.py collectstatic --noinput && gunicorn --timeout 500 tibian_backend.wsgi",
"restartPolicyType": "NEVER",
"restartPolicyMaxRetries": 10
}
}
railway.json at home dir
Brody
Brody3mo ago
change to
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS"
},
"deploy": {
"startCommand": "sleep 3 && python manage.py migrate && python manage.py collectstatic --noinput && gunicorn --timeout 500 tibian_backend.wsgi",
"restartPolicyType": "NEVER",
"restartPolicyMaxRetries": 10
}
}
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS"
},
"deploy": {
"startCommand": "sleep 3 && python manage.py migrate && python manage.py collectstatic --noinput && gunicorn --timeout 500 tibian_backend.wsgi",
"restartPolicyType": "NEVER",
"restartPolicyMaxRetries": 10
}
}
Nahasco
Nahasco3mo ago
ok ill test that thanks for helping so far.. ok this error is weird django.core.exceptions.ImproperlyConfigured: The database name 'railwaypostgresql://postgres:FB-2C-FgdaFF21FGDa5fEBFE43-ecC-D@postgres-9l4v.railway.internal:5432/railway' (105 characters) is longer than PostgreSQL's limit of 63 characters. Supply a shorter NAME in settings.DATABASES. I think the env var arent correct ill fix that ok it seems to be working well
Brody
Brody3mo ago
awesome
Nahasco
Nahasco3mo ago
why is that, may you explain?
Brody
Brody3mo ago
why the 3 second sleep is needed?
Nahasco
Nahasco3mo ago
yes, and why is it not needed in the normal db url
Brody
Brody3mo ago
the private network's dns resolver is not available for the first 3 or so seconds
Nahasco
Nahasco3mo ago
Oh would have never figured that on my own thank you! i really appreciate your help
Brody
Brody3mo ago
happy to help!