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

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

问题描述

Hello StackOverFlowers :) 到目前为止,我一直在同一个微型 EC2 实例上运行我的 Django 后端和我的 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 后端,另一个使用我的 PostgreSQL 数据库,我使用 pgadminII 来管理它.两个实例使用相同的安全组并打开所有相同的端口.我已将一个弹性 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.

现在我知道在 settings.py 中我需要将HOST"更改为 PostgreSQL 实例的地址.但我不太确定该放什么.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 代码.

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 :)

推荐答案

亚马逊支持

因此,在 David Wolever 的回复之后,我最终也与亚马逊的工作人员进行了交谈.以防万一你们中的任何人再次遇到这篇文章.仅使用内部服务器 IP 是不够的,但这是一个好的开始.如果您为 Postgresql 实例(最好是 Natty Narwhal)运行 Ubuntu,请确保编辑 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,在我们的测试中,它被证明是在 Natty Narwhal 上运行的最一致和最可靠的 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.

转到 Amazon 管理控制台并确保两个实例都在同一个安全组下.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 实例的终端上执行此操作)

sudo vim postgresql.conf

sudo vim postgresql.conf

按键盘上的i"开始进行更改.使用向下箭头键,直到遇到

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

LISTEN_ADDRESSES:'本地主机'

去掉前面的hash标签,而不是'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 和本地主机地址,并将它们全部放在一个引号中.

完成更改后,按 ESC 并按 ZZ(大写两次以保存更改)

sudo 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

再次按键盘上的i"并按照我上面的方式对其进行更改.

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 数据库的用户登录名.

完成更改后,按 ESC 并按 ZZ(大写两次以保存更改)

在终端中使用此命令重新启动 postgresql 服务器:

Restart the postgresql server using this command in the terminal:

sudo/etc/init.d/postgresql restart

恭喜!服务器已启动并正在运行,但还有最后一步.

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! :)

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

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