TJB
TJB

Reputation: 3846

How do you add a pre-existing database to Postgres?

I am using Postgres in my production server that houses my Django application. I am trying to create a database in psql that already exists. I have the host name, username, password, port, all that good stuff, but after reading Postgres' documentation it seems that there is no way to add in a prexisting database.

Upvotes: 1

Views: 1560

Answers (1)

Abel Osorio
Abel Osorio

Reputation: 923

No, you cannot create a database if it's already created.

It seems like you have a dump (a .sql file) that you want to load in your production server.

By default pg_dump will not include the CREATE DATABASE statement into the .sql file so you should be able to load the .sql file just doing:

psql -h <DB_HOST> -U <DB_USER> -p <DB_PORT> -W <DB_NAME> < <PATH_TO_YOUR_SQL_FILE>

Example:

psql -h localhost -U db_user -p 5432 -W db_name < /tmp/my_dump.sql

That's it.

Let me know if you have any issues doing that.

Cheers

Upvotes: 4

Related Questions