I have a Project-A, and I'm starting Project-B. I want to use Project-A as a starting point. So I copied the files, but how can I duplicate the database? Thank you!
-
What do you mean, "duplicate the database"? – jvillian Dec 10 '17 at 22:23
-
1I am assuming you mean copy a database? If so check out this https://stackoverflow.com/questions/1237725/copying-postgresql-database-to-another-server – jdgray Dec 10 '17 at 22:35
3 Answers
The exact command depends on what type of database you are copying from and too, also on whether you want to copy the structure only or the structure and the content.
A general way to do this would be to export the Project-A database into an SQL file, then run that SQL file through the project-B database. The SQL file can store the structure, or the content or both - you choose when you do the export.
Postgresql uses the command pg_dump
to export to SQL. The accepted answer in the question linked to in jdgray's comment shows how the output of pg_dump
can be piped directly into the second database so that no intermediate file is created.

- 4,717
- 2
- 29
- 43
To get your database
pg_dump -Fc mydb > db.dump
To restore it:
pg_restore -d <you_new_db_name> /db.dump
This is assuming you are going from pg to pg. All data and structure and relationships will come over with this. I would suggect using pgadmin4 to make the new db before hand so you can just import over to it. In your database.yml change the db name.
If you need addition stuff, like declaring which ip address your db is on use the -p flag. Here is the link to more flags (Postgres v 9.6): Postgres Link

- 354
- 1
- 12
I just edited the db name from database.yml and ran rake db:create db:migrate

- 1,061
- 1
- 12
- 26