getaddrinfo ENOTFOUND postgres.railway.internal
iambull8905
FREEOP

16 days ago

I'm trying to use a population script on the production version of my postgreSQL database. However, upon doing so, I get the error in the title.

#!/usr/bin/env node

const { Client } = require("pg");

const SQL = `CREATE TABLE IF NOT EXISTS messages (

id INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,

text TEXT,

username VARCHAR( 255 ),

added TIMESTAMPTZ DEFAULT NOW()

);

INSERT INTO messages (text, username)

VALUES

('Hii im so cool!', 'a' ),

('ok', 'b'),

('How flattering', 'a')

`;

async function main() {

console.log("Seeding");

const client = new Client({

connectionString: process.argv[2],

});

await client.connect();

await client.query(SQL);

await client.end();

console.log("done");

}

main();

Unsure why it works with my local version but not if I try the production one. The public database URL is given: postgresql://${{PGUSER}}:${{PGPASSWORD}}@${{RAILWAY_TCP_PROXY_DOMAIN}}:${{RAILWAY_TCP_PROXY_PORT}}/${{PGDATABASE}} but I haven't been given the RAILWAY_TCP_PROXY_DOMAIN or RAILWAY_TCP_PROXY_PORT environment variables so that I can run node ./db/populatedb.js with the public database URL.

I've also tried installing the railway cli, linking it and using the ssh tunnel. However, my connection times out so I can't use my private database URL either.

Solved$10 Bounty

Pinned Solution

The command works on my end, I just tried it. Maybe try to regenerate the ssh key; delete the file id_ed25519.pub, and id_ed25519, run the command ssh-keygen -t ed25519 (with no passphrase when it asks you, leave it empty), and try again.

23 Replies

Railway
BOT

16 days ago

The postgres.railway.internal hostname only resolves inside Railway's private network, so it cannot be reached from your local machine. Your Postgres service does not have TCP Proxy enabled yet, which is why the RAILWAY_TCP_PROXY_DOMAIN and RAILWAY_TCP_PROXY_PORT variables are missing. Enable TCP Proxy on your Postgres service under its settings in the Networking section for port 5432, and the public connection URL will be generated automatically for use with your local script.


Status changed to Awaiting User Response Railway • 16 days ago


Railway

The `postgres.railway.internal` hostname only resolves inside Railway's private network, so it cannot be reached from your local machine. Your Postgres service does not have TCP Proxy enabled yet, which is why the `RAILWAY_TCP_PROXY_DOMAIN` and `RAILWAY_TCP_PROXY_PORT` variables are missing. Enable TCP Proxy on your Postgres service under its settings in the Networking section for port 5432, and the public connection URL will be generated automatically for use with your local script.

iambull8905
FREEOP

16 days ago

Its already done?

image.png

Attachments


Status changed to Awaiting Railway Response Railway • 16 days ago


Railway
BOT

16 days ago

To correct our earlier message, TCP Proxy is indeed already enabled on your Postgres service. The public connection string is in the DATABASE_PUBLIC_URL variable on your Postgres service's Variables tab. Copy that value and pass it to your script: node ./db/populatedb.js "<paste DATABASE_PUBLIC_URL here>".


Status changed to Awaiting User Response Railway • 16 days ago


iambull8905
FREEOP

16 days ago

Lovely, that worked now but how do I now use my private database field so that I avoid paying the egress fees. Do I need an ssh tunnel for this and if so, why does mine keep timing out whilst trying to connect?


Status changed to Awaiting Railway Response Railway • 16 days ago


Railway
BOT

16 days ago

The private URL (postgres.railway.internal) only resolves between services deployed inside the same Railway project, so your local machine cannot use it directly. To run your script locally through an SSH tunnel (avoiding the public TCP Proxy and its egress fees), use railway connect postgres --tunnel-only, which prints local connection details you can pass to your script. If that tunnel is timing out, make sure your CLI is up to date and linked to the correct project and environment via railway link.


Status changed to Awaiting User Response Railway • 16 days ago


iambull8905
FREEOP

16 days ago

can you link what page in the documentation you got all this information from?


Status changed to Awaiting Railway Response Railway • 16 days ago


Railway
BOT

16 days ago

The main pages are Private Networking for how .railway.internal hostnames work between deployed services, and railway connect for the CLI tunnel and database connection workflow.


Status changed to Awaiting User Response Railway • 16 days ago


iambull8905
FREEOP

16 days ago

Timed out waiting for the SSH tunnel to become ready on 127.0.0.1 even after checking cli version (I installed it via npm 60 seconds ago) and using railway link on my project then postgres


Status changed to Awaiting Railway Response Railway • 16 days ago


Railway
BOT

16 days ago

This thread has been opened as a bounty so the community can help solve it.

Status changed to Open Railway • 16 days ago


If you want to populate/seed your database, you can just use the public URL from your local machine, and it wouldn't be considered egress traffic, as the data is counted as ingress instead (received by your postgres service). Egress only counts if you use the public URL of the db to retrieve data/run backups etc. For the private network, it's only available between services in the same project.


darseen

If you want to populate/seed your database, you can just use the public URL from your local machine, and it wouldn't be considered egress traffic, as the data is counted as ingress instead (received by your postgres service). Egress only counts if you use the public URL of the db to retrieve data/run backups etc. For the private network, it's only available between services in the same project.

iambull8905
FREEOP

16 days ago

So if I want to retrieve data on my production db whilst on my local computer with the private URL, how come I can't create an SSH tunnel into doing so? What else am I supposed to do?


andywithcamera
HOBBYTop 10% Contributor

16 days ago

Hey. First, DNS for the private network only resolves between services in the same project/environment, so check the app and the database are actually in the same environment (not staging vs production). Second, if your Dockerfile uses an alpine node image you need to set ENABLE_ALPINE_PRIVATE_NETWORKING=true on the app service or the .railway.internal domains won't resolve at all, that's in the Railway docs on private networking. Third, even with DNS fine the database service might not be up yet when your app starts, so a small sleep before your start command (like sleep 5 && node index.js) helps rule that out. Quick sanity check: run getent hosts postgres.railway.internal (or ping it) from the app's deploy logs/shell, if that fails it's DNS/networking, if it resolves then it's your connection string. Also worth posting which node base image you're using and whether it's Dockerfile or nixpacks, that changes the answer.


iambull8905

So if I want to retrieve data on my production db whilst on my local computer with the private URL, how come I can't create an SSH tunnel into doing so? What else am I supposed to do?

You can achieve this by using the command: railway connect postgres --tunnel-only like the Railway bot said, which will give you a postgres connection URL that you can use. If you're timing out, just make sure that you are using the latest railway cli. Check the installation guide: https://docs.railway.com/cli#installing-the-cli to update to the latest version depending on your OS.


iambull8905
FREEOP

16 days ago

I'm not using a dockerfile or nixpacks. I'm trying to understand how I can avoid using my public URL of the db if I'm trying to access the production database via my local PC. The connection string (the private one) is from the DATABASE_URL environmental variable that Railway provided me but by connecting to it, it gives that error. I'm using nodejs with express only.


darseen

You can achieve this by using the command: `railway connect postgres --tunnel-only` like the Railway bot said, which will give you a postgres connection URL that you can use. If you're timing out, just make sure that you are using the latest railway cli. Check the installation guide: https://docs.railway.com/cli#installing-the-cli to update to the latest version depending on your OS.

iambull8905
FREEOP

16 days ago

Hi, I am using the latest CLI because I installed it with npm a minute before typing that it timed me out here


Can you share the exact error message you're receiving (if any), when trying to run the command?


darseen

Can you share the exact error message you're receiving (if any), when trying to run the command?

iambull8905
FREEOP

16 days ago

Using SSH key from file /home/iambull/.ssh/id_ed25519.pub: iambull@GAMING-PC

Timed out waiting for the SSH tunnel to become ready on 127.0.0.1


iambull8905

Using SSH key from file /home/iambull/.ssh/id_ed25519.pub: iambull@GAMING-PC Timed out waiting for the SSH tunnel to become ready on 127.0.0.1

Is the database online and healthy? If yes, can you install the railway cli using the command: bash <(curl -fsSL railway.com/install.sh) and try again?


darseen

Is the database online and healthy? If yes, can you install the railway cli using the command: `bash <(curl -fsSL railway.com/install.sh)` and try again?

iambull8905
FREEOP

16 days ago

yes the db is fine, here's a screenshot of it still happening

image.png

Attachments


The command works on my end, I just tried it. Maybe try to regenerate the ssh key; delete the file id_ed25519.pub, and id_ed25519, run the command ssh-keygen -t ed25519 (with no passphrase when it asks you, leave it empty), and try again.


darseen

The command works on my end, I just tried it. Maybe try to regenerate the ssh key; delete the file `id_ed25519.pub`, and `id_ed25519`, run the command `ssh-keygen -t ed25519` (with no passphrase when it asks you, leave it empty), and try again.

iambull8905
FREEOP

16 days ago

Yay, that worked. Thank you so much! Finally, can you explain what this tunnel will allow me to do and how this will help me use the private URL of the database when I'm querying/adding data?


iambull8905

Yay, that worked. Thank you so much! Finally, can you explain what this tunnel will allow me to do and how this will help me use the private URL of the database when I'm querying/adding data?

The tunnel securely connects your local machine into your db's private network, bypassing the public connection (I believe), which is more secure, you can use it to run migrations, seed your db etc. from your local machine. As for the private network, it's the recommended way of communication between services in the same project, as it reduces latency, and avoids egress fees.


darseen

The tunnel securely connects your local machine into your db's private network, bypassing the public connection (I believe), which is more secure, you can use it to run migrations, seed your db etc. from your local machine. As for the private network, it's the recommended way of communication between services in the same project, as it reduces latency, and avoids egress fees.

iambull8905
FREEOP

16 days ago

So just to confirm, by using the tunnel, I won’t run into errors like the one in this post’s title?


iambull8905

So just to confirm, by using the tunnel, I won’t run into errors like the one in this post’s title?

Correct, the issue in the title is unrelated to the tunnel.


Status changed to Solved iambull8905 • 16 days ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...