5

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?

Rve
  • 107
  • 2
  • 9
  • Are you able connect via psql to the remote database? If you don't know how try something like this from command line tool: psql -h ip_address -d name_of_the_database -U username – Bulva Apr 26 '18 at 18:37
  • I get a simple answer: psql: could not connect to server: Connection refused Is the server running on host "46.101.165.162" and accepting TCP/IP connections on port 5432? – Rve Apr 26 '18 at 19:00
  • So there is a problem with connection to database, which is not related to Django. Settings looks fine. Did you restart the postgresql or server (try "sudo reboot now")? Then try connect via psql again. What is the version of your postgresql? – Bulva Apr 26 '18 at 19:04
  • If restart will not work try set pg_hba.conf connections like this: host all all 0.0.0.0/0 md5 #ipv4 range host all all ::0/0 md5 #ipv6 range host all all all md5 #all ip – Bulva Apr 26 '18 at 19:08
  • Nope, still no use. I tried both variants. – Rve Apr 26 '18 at 19:30
  • Ok. If you run "netstat -na" there is a row which shows listening on the port 5432? Is port really open (sudo ufw status shows port 5432)? Is this PostgreSQL 10? – Bulva Apr 26 '18 at 19:40
  • Sorry, forgot to mention the last time - I'm running PostgreSQL 9.5.12. If I check sudo ufw status, it shows 5432 allow anywhere, 5432 (v6) allow anywhere, 5432/tcp (v6) allow anywhere (v6), and 5432/tcp allow anywhere. If I check netstat -na, it doesn't show any 5432. – Rve Apr 26 '18 at 20:05
  • So, that's the problem. You should see something like: tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN – Bulva Apr 26 '18 at 20:13
  • 1
    I am using the same version. Are you sure that you set up good listen_address='*' in postgresql.conf (without # on the start)? After every change you have to restart postgresql service "sudo service postgresql restart". In the same file, is there port 5432? Sometimes postgresql runs on 5433 – Bulva Apr 26 '18 at 20:24
  • You said the website is working but you can check if PostgreSQL Is really running with "sudo service postgresql status'? If it says running you have to check logs. I don't see any other options than check the logs for some error /var/logs/ and check sys logs – Bulva Apr 26 '18 at 21:27
  • Wow, I am speechles.. The whole time I was dealing with the problem I simply missed the part to check if there is a # in front of listen address. Much thanks! Problem solved. – Rve Apr 27 '18 at 12:14
  • Nice. Glad to help. Answer can help the others to debug this problem – Bulva Apr 27 '18 at 13:15

2 Answers2

14

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

What should I check if remote connect to PostgreSQL not working?

  1. 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.

  2. 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 = '*'
    
  3. After every step you should restart Postgresql service:

    sudo service postgresql restart
    
  4. After step 2 you should see port 5432 (or 5433) in listening address after netstat command:

    netstat -ntlp
    
  5. 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
    
  6. 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?.

Bulva
  • 1,208
  • 14
  • 28
0

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
user3821178
  • 353
  • 6
  • 15