Hello StackOverFlowers :) up until now I have been running my Django back end and my PostgreSQL database on the same micro EC2 instance.
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.
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?
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 :)
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.
You can usually find those two files at: /etc/postgresql/8.4/main/(pg_hba.conf or postgresql.conf)
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.
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.
STEP 1
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!
STEP 2
(Do this on the terminal on the postgresql instance)
sudo vim postgresql.conf
Press "i" on your keyboard to start making changes. Use the down arrow key until you come across
LISTEN_ADDRESSES: 'localhost'
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.
EXAMPLE:
LISTEN_ADDRESSES: 'private ip, 127.0.0.1'
Make sure you separate the private ip and the local host address by a comma and leave it all under one quotation.
Once you have made the changes, press ESC and press ZZ (twice in caps to save the changes)
STEP 3
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
Once again, press 'i' on your keyboard and make the changes to it exactly the way I have it above.
You'll notice under IPv6, I have django_db and django_login, change it to the name of your postgresql database and your user login that you use for your postgresql database respectively.
Once you have made the changes, press ESC and press ZZ (twice in caps to save the changes)
STEP 4 (Almost done Hi5!)
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.
STEP 5
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.
}
Under 'HOST': ' ' , change it to 'HOST': 'whatever the private IP of the Postgresql instance is'
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
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! :)
You don't need an elastic IP for your Postgres instance. Just use its internal IP. The internal IP is available from the management console, or by running ifconfig
.