Reputation: 117
I'm trying to set up a remote connection through PostgreSQL running on my server , based on Ubuntu 16.04. So far, when I click on the Save button on pgAdmin, it sort of freezes, does nothing. After typing .../manage.py runserver My_droplet_IP:5432, I try the webpage, and it is accessible.
I followed this tutorial after creating my droplet. https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-16-04
Then I edited the settings.py; pg_hba.conf; postgresql.conf files
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresqlpsycopg2',
'NAME': '.....',
'USER': '....',
'PASSWORD': '....',
'HOST': '127.0.0.1',
'PORT': '5432',
STATICROOT = os.path.join(BASE_DIR, 'static/') - at the end of the page
And, ofcourse changed the ALLOWED HOSTS = ['....'] with my droplet ip aswell.
postgresql.conf listen_address is set to '*'
pg_hba.conf file:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::1/128 md5
Also allowed firewall, and made an exception to 5432 to be allowed.
Any ideas?
Upvotes: 6
Views: 11540
Reputation: 373
In case you are using GCP remember to set the firewall rule inside GCP to allow that port, it might save you some hours of debugging.
Upvotes: 0
Reputation: 1248
First of all test if you can connect to the database via psql:
psql -h ip_address -d name_of_the_database -U username
If you get connection refused error you had to set up something wrong and check the What should I check if remote connect to PostgreSQL not working?
psql: could not connect to server: Connection refused Is the server running on host ip_address
Check the authentication configuration in pg_hba.conf
Usually located on linux - /etc/postgresql/version/main/pg_hba.conf
.
You should allow authentication for client for specific IP all from all IP addresses:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 0.0.0.0/0 md5
# IPv6 local connections:
host all all ::0/0 md5
#all ips
host all all all md5
More information how to set up pg_hba.conf
you can find in documentation.
Then you should set up listening on specific port.
You have to find the postgresql.conf
. Usually located /etc/postgresql/9.1/main/postgresql.conf
) file and change the line with listen_address from:
#listen_address = ''
to (don't forget remove # which means comment):
listen_address = '*'
After every step you should restart Postgresql service:
sudo service postgresql restart
After step 2 you should see port 5432 (or 5433) in listening address after netstat command:
netstat -ntlp
After that you have to open port for PostgreSQL in firewall:
sudo ufw allow 5432
You can check firewall settings with (you should see 5432 in the list):
sudo ufw status
If any of the previous step doesn't work you should check if PostgreSQL is not running on different port (usually 5433) and repeat the previous steps.
This happens very often when you have more running versions of PostgreSQL or you upgrade database and forgot stop the previous version of PostgreSQL.
If you have problems to find configuration files you can check this thread Where are my postgres *.conf files?.
Upvotes: 16