Setting up Django and PostgreSQL for two different EC2 instances

Hi StackOverFlowers :) so far I have run my end of Django and my PostgreSQL database in the same micro EC2 instance.

I installed two EC2 instances, one with my back end django, and the other with my PostgreSQL base, on which I use pgadminII to manage it. Both instances use the same security group and open all the same ports. I connected Elastic IP to the Django instance and another Elastic IP to the Postgresql instance.

Now I know that in settings.py I need to change "HOST" to the address of the PostgreSQL instance. But I'm not quite sure what to say. Am I installing Elastic IP on a PostgreSQL instance?

I did some research, and many sources say that I need to enter the internal IP address of the PostgreSQL instance server. If so, how can I find the internal IP address of the server and enter it in "HOST"? I 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 more understandable for you and for others :)

+4
source share
2 answers

AMAZON SUPPORT

So, I ended up talking with the guys in the Amazon, and after David Woleverโ€™s answer. Just in case, any of you will meet this post again. Using an internal IP server is not enough, but it is a good start. If you are using Ubuntu for your Postgresql instance (preferably Natty Narwhal), make sure that you are editing the pg_hba.conf and postgresql.conf files.

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

Remember, we use Postgresql 8.4 in our stack, it turned out to be the most consistent and reliable version of Postgresql to work on Natty Narwhal during our tests.

For different versions of Postgresql (9.1, 9.0, etc.), the directory in which you can find these two files does not match the above. Make sure you know the appropriate directory for these files.

STEP 1

Go to the Amazon management console and verify that both instances are under the same security group. Postgresql and Django use 5432 and 8000 by default, so make sure these two ports are open!

STEP 2

(do it on the terminal in the postgresql instance)

sudo vim postgresql.conf

Press โ€œiโ€ on the keyboard to start making changes. Use the DOWN ARROW KEY until you meet

LISTEN_ADDRESSES: "localhost"

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

Example:

LISTEN_ADDRESSES: 'private ip, 127.0.0.1'

Make sure you separate the private IP address and the local host address with a comma and leave it all under one quote.

After making the changes, press ESC and press ZZ (twice in the caps to save the changes)

STEP 3

sudo vim pg_hba.conf

Use the down arrow key until you come across something similar to 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 "i" on the keyboard again and make changes to it exactly as I above it.

You will notice that under IPv6 I have django_db and django_login, change it to the name of your postgresql database and your user name, which you use for your postgresql database, respectively.

After making the changes, press ESC and press ZZ (twice in the caps to save the changes)

STEP 4 (almost done Hi5!)

Restart the postgresql server with this command in the terminal:

sudo / etc / init.d / postgresql restart

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

STEP 5

Launch your instance of Django EC2, go into your settings.py file and find 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. } 

In the "HOST": '' section, change it to "HOST": "no matter the private IP address of the Postgresql instance

Save the changes. Open a terminal and find the directory where the manage.py file is located. Once you are in this directory, run the following command: ./manage.py syncdb

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

If you have any questions, I will be more than happy to help! Leave a comment below and I will get back to you as soon as possible! :)

+8
source

You do not need a flexible IP address for your Postgres instance. Just use your internal IP address. The internal IP address can be obtained from the management console or using ifconfig .

+4
source

Source: https://habr.com/ru/post/1446542/


All Articles