设置Django和PostgreSQL的两个不同的EC2实例 [英] Setting Up Django and PostgreSQL on two different EC2 instances

查看:183
本文介绍了设置Django和PostgreSQL的两个不同的EC2实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好StackOverFlowers :)到现在为止,我一直在运行我的Django的后端和我在同一个微EC2实例PostgreSQL数据库。

Hello StackOverFlowers :) up until now I have been running my Django back end and my PostgreSQL database on the same micro EC2 instance.

我已经设置了两个EC2实例,一个是我的Django的后端,并与我的,我用pgadminII来管理PostgreSQL数据库的另一个实例。这两个实例使用相同的安全组,并拥有所有相同的端口开放。我已经附加了弹性IP到我的Django的实例和另一个弹性IP到我的PostgreSQL的实例。

I've set up two EC2 instances, one with my django back end and the other instance with my PostgreSQL database on which I use pgadminII to manage it. Both instances use the same security group and have all the same ports open. I've attached an Elastic IP to my Django instance and another Elastic IP to my Postgresql instance.

现在我知道我需要改变HOST到PostgreSQL实例的地址的settings.py。但我不能肯定放什么。难道我把PostgreSQL的实例的弹性IP?

Now I know in the settings.py I need to change the 'HOST' to the address of the PostgreSQL instance. But I'm not quite sure what to put. Do I put the Elastic IP of the PostgreSQL instance?

我已经做了一些研究,许多消息来源说,我需要把PostgreSQL的实例的内部服务器的IP地址。如果是这样的情况下,我怎么能找到的内部服务器的IP地址,并输入该到HOST?我复制并粘贴下面清晰的settings.py code。

I've done some research and many sources say I need to put in the internal server IP address of the PostgreSQL instance. If that's the case how can I find the internal server IP address and input that into the 'HOST'? I've copied and pasted the settings.py code below for clarity.

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', 
'NAME': 'django_db', 
'USER': 'django_login',
'PASSWORD': 'password', 
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}

感谢您的帮助,如果我不够清晰,请发表评论,让我知道,这样我可以更清楚地为您和其他人:)

Thanks for the help, if I'm not clear enough, please comment and let me know so that I can make it clearer for you and for others :)

推荐答案

所以,我结束了之后还有大卫Wolever的答复说话的家伙亚马逊。万一任何你遇到这个帖子了。单独使用内部服务器的IP是不够的,它不过是一个良好的开端。如果您正在运行Ubuntu为你的PostgreSQL实例(preferably纳蒂独角鲸),请确保您编辑的pg_hba.conf和postgresql.conf中的文件。

AMAZON SUPPORT

So I ended up talking to the guys at Amazon as well after David Wolever's reply. Just in case any of you come across this post again. Using the internal server IP alone isn't enough, it is however a good start. If you are running Ubuntu for your Postgresql instance (preferably Natty Narwhal), make sure you edit the pg_hba.conf and postgresql.conf files.

您通常可以找到这两个文件为:/etc/postgresql/8.4/main/(pg_hba.conf或postgresql.conf里)

You can usually find those two files at: /etc/postgresql/8.4/main/(pg_hba.conf or postgresql.conf)

你要知道,我们在我们的堆栈使用PostgreSQL 8.4,它被证明是的PostgreSQL是最一致的,可靠的版本在我们的测试上纳蒂独角鲸运行。

Mind you, we are using Postgresql 8.4 in our stack, it proved to be the most consistent and solid version of Postgresql to run on Natty Narwhal during our tests.

有关的不同版本的Postgresql(9.1,9.0等等)的目录而可以找到这两个文件都没有与上述相同列出。确保你知道正确的目录中对这些文件。

For different versions of Postgresql (9.1, 9.0 etc..) the directory to which you can find these two files aren't the same as listed above. Make sure you know the proper directory to these files.

进入亚马逊管理控制台,并确保这两种情况下是相同的安全组下。 PostgreSQL和Django的使用5432和8000在默认情况下,所以一定要确保你有这两个端口打开!

Go to the Amazon Management Console and make sure both instances are under the same security group. Postgresql and Django use 5432 and 8000 by default, so make sure you have those two ports open!

(做这个的PostgreSQL的实例,在终端上)

须藤的vim的postgresql.conf

sudo vim postgresql.conf

preSS我在键盘上开始进行更改。使用向下箭头键,直到你遇到

Press "i" on your keyboard to start making changes. Use the down arrow key until you come across

的listen_addresses:'localhost'的

摆脱在前面的散列标签,而不是本地主机,你的PostgreSQL实例添加专用IP (你可以找到你的EC2管理控制台上的私有IP),然后你还必须添加127.0.0.1。

Get rid of the hash tag in the front, and instead of 'localhost', add the private IP of your postgresql instance (you can find the private ip on your EC2 management console) and you must also add 127.0.0.1.

示例:

的listen_addresses:私有IP,127.0.0.1

LISTEN_ADDRESSES: 'private ip, 127.0.0.1'

请确保您用逗号分隔的私有IP和本地主机地址,并把它都在一个报价。

在你所做的更改, preSS ESC和preSS ZZ(两次盖帽保存更改)

须藤的vim的pg_hba.conf

sudo vim pg_hba.conf

使用向下箭头键,直到你遇到的东西,看起来像这样:

Use the down arrow key until you come across something that looks like this:

           Database administrative login by UNIX sockets
  local   all         postgres                          ident

  # TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

  # "local" is for Unix domain socket connections only
  local   all         all                               md5
  # IPv4 local connections:
  host    all         all         127.0.0.1/32          trust
  # IPv6 local connections:
  host    all         all         ::1/128               md5
  local   django_db   django_login                      md5
  host    replication postgres    127.0.0.1/32          md5
  host    replication postgres    ::1/128               md5

再次,preSS'我'你的键盘上进行更改,以它完全的方式,我把它上面。

Once again, press 'i' on your keyboard and make the changes to it exactly the way I have it above.

您会注意到了IPv6下,我有django_db和django_login,将其更改为您的PostgreSQL数据库的您为您的PostgreSQL数据库分别使用名称和您的用户登录。

在你所做的更改, preSS ESC和preSS ZZ(两次盖帽保存更改)

在终端使用以下命令重新启动PostgreSQL服务器:

Restart the postgresql server using this command in the terminal:

须藤/etc/init.d/postgresql的重启

恭喜!服务器启动并运行,但是还有最后一步。

Congrats! The server is up and running, however there is one last step.

打开你的Django EC2实例,去你的settings.py,寻找这样的:

Fire up your Django EC2 instance, go to your settings.py and look for this:

 DATABASES = {
 'default': {
 'ENGINE': 'django.db.backends.postgresql_psycopg2', 
 'NAME': 'django_db', 
 'USER': 'django_login',
 'PASSWORD': 'password', 
 'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
 'PORT': '', # Set to empty string for default. Not used with sqlite3.
 }

在HOST:,将其更改为HOST:无论PostgreSQL的实例的私有IP为

Under 'HOST': ' ' , change it to 'HOST': 'whatever the private IP of the Postgresql instance is'

保存更改。打开一个终端,找到目录,你的manage.py文件。 一旦你在该目录中运行以下命令: ./ manage.py的SyncDB

Save the changes. Open up a terminal and find the directory to where your manage.py file is. Once you are in that directory run the following command: ./manage.py syncdb

这些将创造一切为你在Django创建的模型所需的表。恭喜你,你已经成功创建了数据库实例和你的Django实例之间的联系。

These will create all the necessary tables for the models you created in Django. Congratulations, you have successfully created a link between your Database instance and your Django instance.

如果您有任何问题,我会很乐意提供帮助!发表评论下面,我会尽快给您尽快! :)

If you have any questions, I'd be more than happy to help! Leave a comment below and I'll get back to you ASAP! :)

这篇关于设置Django和PostgreSQL的两个不同的EC2实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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