11

I have been following the documentation Heroku docs on the subject and have gone through this and tried variations on what it says to do to for

Direct database-to-database copies

I am convinced the docs are wrong or I have some setting or somthing wrong (do have admin access). I have also read some scattered things on SO that the command in the heroku CLI for how to do this has changed.

When I run this command (using the credentials for my heroku DB)

heroku pg:copy sushi::ORANGE GREEN --app sushi-staging

After I confirm the message:

WARNING: Destructive action ▸ This command will remove all data from

and confirm. Then I get an error that says:

Heroku PostgreSQL database must be source or target

What is the corrrect way to copy data from a postgresql database in one app to a postgresql database in another app on heroku?

7 Answers 7

11

The proper way to do this is:

heroku pg:copy [app name to copy from]::[app DB name] [DB to copy to] --app [app name to copy to]

So if you want to copy from database 'Orange' on app 'sushi' to database 'Green' on app 'sushi-staging', the command would be what you had:

heroku pg:copy sushi::Orange Green --app sushi-staging

Most of the time, you will only have one database on an app and the name won't be a color. It will be named 'DATABASE_URL'. You can find your database names in the config vars of the app. I had only one DB, so I had to do:

heroku pg:copy sushi::DATABASE_URL DATABASE_URL --app sushi-staging

Hope that helps

2
  • Will this destroy/remove the data in sushi::DATABASE_URL ?
    – jeanmw
    Nov 30, 2021 at 19:58
  • @jeanmw No, it will not remove the data in sushi::DATABASE_URL. I know it is a bit late but I am answering in case anyone new might get into the same issue. Sep 12, 2022 at 10:28
5

This was seriously not clear from the docs. And sombody at with heroku should make this clear.

heroku pg:copy ::DATABASE_URL -a ... 

You actually write the database as the string representation for the to-database-url but you use the environment variable name DATABASE_URL

2
  • 1
    Can you clarify your solution? I'm not sure if you mean that the from-database_url should be replaced with DATABSE_URL? And what goes in the ... ?
    – Stan James
    Jan 30, 2018 at 22:32
  • 2
    You define DATABASE_URL as an environment variable, and then you set it equal to the path to your heroku database there. You then literally include DATABASE_URL, and the command heroku pg:copy ::DATABASE_URL -a will automatically map to the defined environment variable. Jan 31, 2018 at 19:55
2

This worked for me:

heroku pg:copy [origin databasee URI] DATABASE_URL --app [app-name]

1

I had same issue, resolved by using HEROKU_POSTGRESQL_TEAL_URL instead of the DATABASE_URL:

heroku pg:copy postgresql-rugged-XXXX postgresql-closed-XXX -a app-name

Note: The two databases are attached here to the same app (app-name, in my case).

Now, how do you find the HEROKU_POSTGRESQL_TEAL_URL? You can find it here:

Heroku add-ons

1
  • This is the correct answer when dbs are in same app.
    – Rezoan
    Jul 15, 2021 at 3:49
1

The actual FULL command that worked for me, DATABASE_URL not being replacement for anything, was:

heroku pg:copy project1::DATABASE_URL project2::DATABASE_URL

heroku automatically fills in the environmental variables.

0

Unfortunately copy didn't work for me so I did a backup/restore operation

heroku pg:backups:capture --app [source-app]

If you run the command below, it will show the available backups from the source-app and its IDs

heroku pg:backups --app=[source-app]

Get your destination database URL using

heroku config:get DATABASE_URL --app [destination-app]

Then you create an env var named DATABASE_URL and add the information from the command above

export DATABASE_URL='postgres://…'

And finally the restore command. (just write DATABASE_URL as this will get the value from the env variable we've created before)

heroku pg:backups:restore [source-app]::[backup-id] DATABASE_URL --app [destination-app]

Update: Exporting the database_urls for both SOURCE and DESTINATION you can use the below to copy between apps

heroku pg:copy [source-app]::SOURCE DESTINATION --app [destination-app]
0

It's a different scenario if you're looking at transferring an app from one heroku account to another. You'd probably want to "transfer ownership". This is possible like so:

  1. You must ensure that the account receiving the new ownership has a credit-card detail (i.e if the original account has an accrued cost)

  2. Ensure you've added the new account receiving ownership as "a collaborator" in the original owners heroku-account. This can be done by clicking on the app (in your dashboard), click on the "Access" tab. Then add a collaborator, by inputting the email address of the heroku-account receiving ownership.

you can follow this guide as well: https://devcenter.heroku.com/articles/transferring-apps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.