您如何将现有数据库添加到Postgres? [英] How do you add a pre-existing database to Postgres?

查看:107
本文介绍了您如何将现有数据库添加到Postgres?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在包含Django应用程序的生产服务器中使用Postgres。我试图在已经存在的psql中创建一个数据库。我拥有主机名,用户名,密码,端口以及所有这些好东西,但是在阅读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.

推荐答案

不,如果数据库已经创建,则无法创建。

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

似乎您要在生产服务器中加载转储文件(.sql文件)。

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

默认情况下 pg_dump 在.sql文件中包含 CREATE DATABASE 语句,因此您应该能够仅加载.sql文件

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>

示例:

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

就是这样。

如果您有任何问题,请告诉我。

Let me know if you have any issues doing that.

欢呼声

这篇关于您如何将现有数据库添加到Postgres?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆