TL;DR:
heroku login
heroku run 'pg_dump $DATABASE_URL' > <filename.sql> --app <heroku-app-name>
PGPASSWORD=$PGPASSWORD psql -h $PGHOST -U $PGUSER -p $PGPORT -d $PGDATABASE -f <filename.sql>
Details:
If you logged in successfully after running heroku login, $DATABASE_URL is read from your Heroku environment, so no need to insert the database url by hand.
<filename.sql>: specify any filename you like. You will use it later to import the database.
<heroku-app-name>: The name of your Heroku (backend) app with the Postgres database.
Provision a new PostgreSQL database in your railway project and grab $PGPASSWORD, $PGHOST, $PGUSER, $PGPORT, and $PGDATABASE from the Variables tab.
This is what it should look like
Example (with dummy credentials):
heroku login
heroku run 'pg_dump $DATABASE_URL' > mydatabasebackup.sql --app my-heroku-backend
PGPASSWORD=hjUasj8hasA6ahsjJash -h containers-us-west-15.railway.app -U postgres -p 6473 -W -F t -d railway mydatabasebackup.sql