Setting up a remote connection to the PostgreSQL 13 database on the Ubuntu 18 LTS server from Windows 10 using the pgAdmin 4 utility, in two ways: connecting using an SSH tunnel and connecting directly to the PostgreSQL server.
? I recommend using an SSH tunnel, which is easy to set up and secure. When using an SSH tunnel, the PostgreSQL port does not open for external connections.
To use SSH tunnel, you need to configure SSH Certificates for login to Ubuntu.
pgAdmin is the most popular and feature rich Open Source administration and development platform for PostgreSQL, the most advanced Open Source database in the world.
PgAdmin official website
Find out the location of PostgreSQL configuration files: postgresql.conf, pg_hba.conf.
ps aux | grep postgres | grep -- -D
Find out the PostgreSQL port with the command:
grep -H '^port' /etc/postgresql/*/main/postgresql.conf
In the example, the PostgreSQL configuration file is located at /etc/postgresql/13/main/postgresql.conf, connection port 5432.
Content
Article in other languages:
?? – Cómo configurar el acceso remoto a la base datos PostgreSQL desde Windows
?? – Как настроить удаленное подключение к БД PostgreSQL из Windows
SSH tunnel to PostgreSQL
Connect to PostgreSQL server using SSH tunnel. When choosing this type of connection, no additional settings are required on the PostgreSQL server.
Setting Up pgAdmin on Windows 10:
- Launch pgAdmin
- Create a new connection to the server: Object -> Create -> Server;
- General tab:
- Name: server name (arbitrary);
- Connection tab:
- Host Name: localhost;
- Port: 5432;
- Maintenance database: mybase;
- Username: postgres;
- SSH Tunnel tab:
- Use SSH tunneling: Yes;
- Tunnel host: myserver-IP;
- Tunnel port: 58222;
- Username: root;
- Authentication: Identity file;
- Identity file: path_key.
? Specify the id_rsa private key from Configuring SSH certificates on the server as a key. How to change SSH port in FAQ.
Direct connection to PostgreSQL
To set up a direct connection to PostgreSQL, we make changes to the postgresql.conf and pg_hba.conf configuration files
PostgreSQL setup
postgresql.conf
The postgresql.conf file is located in the PostgreSQL installation folder.
sudo nano /etc/postgresql/13/main/postgresql.conf
Let’s uncomment or add the line:
listen_addresses = '*'
We have allowed requests from all IP addresses.
? Possible options:listen_addresses = '0.0.0.0'
to listen to all IPv4;listen_addresses = '::'
to listen to all IPv6;listen_addresses = 'your.host.ip.adress'
specific address or list of addresses separated by commas.
pg_hba.conf
The pg_hba.conf file is located in the PostgreSQL installation folder.
sudo nano /etc/postgresql/13/main/pg_hba.conf
Add an entry to the # IPv4 local connections section:
host mybd postgres 41.223.232.15/32 md5
The entry allows the user postgres with IP address 41.223.232.15 to connect to the mybd database using a password.
After the changes, restart the PostgreSQL service.
systemctl restart postgresql
pgAdmin setup
Setting up a direct connection to the PostgreSQL database via the Internet or local network using pgAdmin.
- Start pgAdmin
- Create a new connection to the server: Object -> Create -> Server;
- General tab:
- Name: server name (arbitrary);
- Connection tab:
- Host Name: RemoteServerIP;
- Port: 5432;
- Maintenance database: mybase;
- Username: postgres.
FAQ
ps aux | grep postgres | grep — -D
grep -H ‘^port’ /etc/postgresql/*/main/postgresql.conf
File /etc/ssh/sshd_config, line Port.
How to Setup Remote Access to PostgreSQL Database from Windows was discussed in this article. I hope you can now configure pgAdmin to connect to PostgreSQL (direct connection or using SSH tunnel). However, if you run into any problems while configuring the server or pgAdmin, feel free to write in the comments. I will try to help.
In case users encounter issues or have questions while configuring remote access to PostgreSQL or pgAdmin, where can they seek assistance or get help?
For God’s sake.. can anybody tell how I can locate the postgresql.conf file on Windows. Everybody talks about Windows but gives Linux commands to search it. This is the 10th place I have seen this.
All I want is to uncomment the listen_addresses line and I am using pgadmin 4 on Windows.
Thank you.
Hello Omar.
You are reading the article on how to connect to the PostgreSQL server installed on Linux OS, so you do not find the information you need. 🙂
If you are connecting to PostgreSQL Server installed on Windows 10, the postgresql.conf file is located in the DATA folder of the PostgreSQL instance installed.
For example:
C:\Program Files\PostgreSQL\12\data\postgresql.conf
I hope my answer helped you.