This guide describes how to migrate a database from Railway to Neon Postgres using the pg_dump and pg_restore utilities, which are part of the Postgres client toolset. pg_dump works by dumping both the schema and data in a custom format that is compressed and suitable for input into pg_restore to rebuild the database.
Prerequisites
-
A Railway project containing the Postgres database you want to migrate.
-
A Neon project to move the data to.
For detailed information on creating a Neon project, see Create a project. Make sure to create a project with the same Postgres version as your Railway deployment.
-
pg_dumpandpg_restoreutilities installed on your local machine. These typically come with a Postgres client installation. Install the same version of Postgres client tools as the Postgres version used in your Railway database to ensure compatibility.To check the version of
pg_dumporpg_restore, use the-Voption. For example:pg_dump -V. -
Review the guide on Migrating data from Postgres for more comprehensive information on using
pg_dumpandpg_restore.
Prepare your Railway database
This section describes how to prepare your Railway database for exporting data.
To illustrate the migration workflow, this guide uses the LEGO Database. This database contains information about LEGO sets, parts, and themes. The LEGO database is loaded into Railway using the psql command-line tool.
Retrieve Railway connection details
- Log in to your Railway dashboard and navigate to your project.
- Select your Postgres service in the project canvas.
- Click the Variables tab to view the environment variables.
- Copy the value of the
DATABASE_PUBLIC_URLvariable.tip
To connect to your Railway database from your local machine, ensure the TCP Proxy is enabled for your database service. With the proxy active, you can use the
DATABASE_PUBLIC_URLconnection string to connect to your database from your local machine.
You'll need this connection string for
pg_dumpto connect to the Railway database.Export data with pg_dump
Now that you have your Railway connection details, you can export your data using
pg_dump:pg_dump -Fc -v -d <railway_database_url> --schema=public -f railway_dump.bakReplace
<railway_database_url>with your RailwayDATABASE_URL.This command includes these arguments:
-Fc: Outputs the dump in custom format, which is compressed and suitable for input intopg_restore.-v: Runspg_dumpin verbose mode, allowing you to monitor the dump operation.-d: Specifies the connection string for your Railway database.-f: Specifies the output file name.--schema=public: Specifies the schema to dump. In this case, you only want to back up tables in thepublicschema.
If the command was successful, you'll see output similar to the following:
... pg_dump: saving encoding = UTF8 pg_dump: saving standard_conforming_strings = on pg_dump: saving search_path = pg_dump: saving database definition pg_dump: dumping contents of table "public.lego_colors" pg_dump: dumping contents of table "public.lego_inventories" pg_dump: dumping contents of table "public.lego_inventory_parts" pg_dump: dumping contents of table "public.lego_inventory_sets" pg_dump: dumping contents of table "public.lego_part_categories" pg_dump: dumping contents of table "public.lego_parts" pg_dump: dumping contents of table "public.lego_sets" pg_dump: dumping contents of table "public.lego_themes"Prepare your Neon destination database
This section describes how to prepare your destination Neon Postgres database to receive the imported data.
Create the Neon database
To maintain consistency with your Railway setup, you might want to create a new database in Neon with the same database name used in Railway.
-
Connect to your Neon project using the Neon SQL Editor or a Postgres client like
psql. -
Create a new database. For example, if your Railway database was named
lego, run:CREATE DATABASE lego;
For more information, see Create a database.
Retrieve Neon connection details
-
In the Neon Console, go to your Project Dashboard.
-
Select Connect to open the Connect to your database modal.
-
Select the user and database as needed for your connection. Make sure the Connection pooling toggle is disabled to get a direct connection string.
-
Copy the connection string. It will look similar to this:
postgresql://[user]:[password]@[neon_hostname]/[dbname]?sslmode=require&channel_binding=require
-
Restore data to Neon with pg_restore
Now you can restore your data to the Neon database using
pg_restore:pg_restore -d <neon-connection-string> -v --no-owner --no-acl railway_dump.bakReplace
<neon-connection-string>with your Neon connection string.This command includes these arguments:
-d: Specifies the connection string for your Neon database.-v: Runspg_restorein verbose mode.--no-owner: Skips setting the ownership of objects as in the original database.--no-acl: Skips restoring access privileges for objects as in the original database.
Use the
--no-ownerand--no-acloptions to skip restoring ownership and access control settings from Railway. After migrating the data, review and configure the appropriate roles and privileges for all objects, as needed. For more information, refer to the section on Database object ownership considerations.If the command was successful, you'll see output similar to the following:
pg_restore: connecting to database for restore pg_restore: creating SCHEMA "public" pg_restore: creating TABLE "public.lego_colors" pg_restore: creating SEQUENCE "public.lego_colors_id_seq" pg_restore: creating SEQUENCE OWNED BY "public.lego_colors_id_seq" pg_restore: creating TABLE "public.lego_inventories" pg_restore: creating SEQUENCE "public.lego_inventories_id_seq" ...Verify the migration
After the restore process completes, you should verify that your data has been successfully migrated:
-
Connect to your Neon database using the Neon SQL Editor or psql.
-
Run some application queries to check your data. For example, if you're using the LEGO database, you can run the following:
SELECT * FROM lego_inventory_parts ORDER BY quantity DESC LIMIT 5; SELECT parent_id, COUNT(name) FROM lego_themes GROUP BY parent_id; -
Compare the results with those from running the same queries on your Railway database to ensure data integrity.
-
Other migration options
While this guide focuses on using pg_dump and pg_restore, there are other migration options available:
-
Logical replication
For larger databases or scenarios where you need to minimize downtime, you might consider using logical replication. See the guide on Logical replication for more information.
-
CSV export/import
For smaller datasets or specific tables, you might consider exporting to CSV from Railway and then importing to Neon. See Import data from CSV for more details on this method.
Reference
For more information on the Postgres utilities used in this guide, refer to the following documentation:
Need help?
Join our Discord Server to ask questions or see what others are doing with Neon. For paid plan support options, see Support.








