close
|Docs

railway connect

Connect to a database's interactive shell (psql for Postgres, mongosh for MongoDB, etc.).

Usage

Options

FlagDescription
-e, --environment <ENV>Environment to pull variables from (defaults to linked environment)
--sshTunnel to the database over SSH instead of a public TCP proxy. Auto-enabled when the service has no public proxy URL
--no-sshForce the public TCP proxy path and never fall back to SSH. Conflicts with --ssh
--tunnel-onlyOpen the local tunnel without launching a database client; prints connection details for external tools and holds the tunnel open until Ctrl+C. Implies --ssh
-P, --port <PORT>Local port to bind for the SSH tunnel (defaults to an ephemeral port)

Supported databases

DatabaseClient Required
PostgreSQLpsql
MySQLmysql
Redisredis-cli
MongoDBmongosh

Examples

Connect to database (interactive)

Prompts you to select a database service if multiple exist.

Connect to specific database

Connect to database in specific environment

Point an external GUI client (TablePlus, DBeaver, pgAdmin, ...) at the database

Opens a local SSH tunnel to the database and prints the host, port, user, password, database, and full connection URL, then holds the tunnel open until Ctrl+C. Use this when you want to point a GUI client at the database instead of an interactive shell.

Requirements

  • The appropriate database client must be installed on your machine, unless using --tunnel-only
  • If the database has no public TCP Proxy, railway connect automatically tunnels over SSH instead — no public URL required

How it works

  1. Detects the database type from the service's image
  2. Fetches connection variables (DATABASE_PUBLIC_URL, REDIS_PUBLIC_URL, etc.)
  3. If the service has a public TCP Proxy and --ssh/--tunnel-only weren't passed, launches the appropriate database client directly against it
  4. Otherwise, opens a local SSH tunnel into the service and either launches the database client against the tunnel, or — with --tunnel-only — prints the connection details for an external client and holds the tunnel open