Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remote PostgreSQL connection with pgAdmin

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?

like image 845
Rve Avatar asked Apr 26 '18 18:04

Rve


People also ask

How do you connect to a database in pgAdmin?

2) Connect to PostgreSQL database server using pgAdmin The following illustrates how to connect to a database using pgAdmin GUI application: First, launch the pgAdmin application. Sixth, open the query tool by choosing the menu item Tool > Query Tool or click the lightning icon.

How do I access pgAdmin?

To access the dialog, right click on the server name in the pgAdmin tree control, and select Connect Server… from the context menu. Provide authentication information for the selected server: Use the Password field to provide the password of the user that is associated with the defined server.


1 Answers

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

like image 132
Bulva Avatar answered Nov 03 '22 00:11

Bulva