How to connect to a local postgreSQL database from a remote pc - postgresql

I am pretty new to everything that is database management, remote connection etc... And I need this for a university project.
I have a local postgreSQL database on my Windows computer, which I access through localhost. What I want to do now is connect to this database using my laptop (which is in the same WLAN, don't know if that helps).
I changed the file pg_hba.conf, and added host all all 0.0.0.0/0 md5 and host all all ::0/0 md5. Also in the postgresql.conf file, there is one line listen_addresses = '*' which was the default, but as I understand it it is what it needs to be so a non-localhost can connect to the database.
How can I do this? On my laptop I tried running psql and for the server I put the IPv4 address of my PC(where the local database is stored), but it doesn't connect (Connection timed out)

Related

Remote connection to postgresql in pgAdmin4

I am trying to connect to a database from a remote computer. According to the guides, I configured the files in the pg_hba file by setting 0.0.0.0/0 for IPv4, similarly for IPv6 and for the file in PostgreSQL listen_addresses = '*'. The firewall allowed any connections to port 5432. I use pgAdmin try 4 and in the server settings - the host/addresses for some reason remained on localhost, every time I try to insert my network address there (in X.X.X.0 does not work), a long download occurs, after which the password for the Postgres database user is requested and the error "connecting to the server on "H.H.H.0", port 5432 failed...". At the same time, everything works on localhost. What am I doing wrong?

Connecting remotely to postgreSQL database

I'm trying to connect remotely on pgAdmin to a database hosted on a Windows 10 PC on another network from my personal PC.
Steps taken:
Changed listen_addresses = 'localhost' to listen_addresses = '*' in postgresql.conf file.
Added line host all all 0.0.0.0/0 to pg_hba.conf file.
Restarted postgresql service.
Opened port in firewall for port 5432
Tried to connect to database via pgAdmin on my PC by entering:
Host name: IPv4 address of machine database is hosted on.
Port:5432, Database: postgres, user: postgres, password: password
I can't see any error except a connection timed out error every time, but I don't know why. I've increased the connection timeout duration but still no connection. PostgreSQL service is running and I can connect to the database via pgAdmin just fine on the hosting PC.
Can anyone let me know if I'm skipping a step or anything else I need to add to the config files?
Thanks

Connecting to Postgres from Remote Network

Unable to get data from Postgres from remote network. We have a Postgres DB setup for our MItel System and users from different vlans cannot get data. They can connect to the DB successfully but getting "no records found" when connected. The users that are connect to the same vlan as the DB can connect fine and get all the data (call History) but users from a branch office cannot receive data.
What I have setup in the pg_hba.conf file is:
host all all 10.0.0.0/24 trust (I've tried md5 as well)
host all all 172.0.0.0/24 trust
In the Postgressql.conf I have:
Listen_addresses = '*'
All users are using the same database username and password.
I found out what i needed to put in the pg_hba.conf file. The line needed to read
host all all 0.0.0.0/0 md5
Not quite sure why the network address or subnet didn't work but they didn't. For whatever reason that line worked and all data is flowing. Thanks

Connecting DBeaver to remote PostgreSQL DB via Unix socket

I recently installed https://dbeaver.io/ on a Windows PC and wish to access a database on a remote Linux server from it.
My Linux username is my_username and I also have a system user psql_user. I also have two existing PostgreSQL databases with the same name as their respective user. Typically, only the psql_user is used and is access by a php-fpm pool listening to a Unix socket and running as user psql_user, and as such have configured /var/lib/pgsql/12/data/pg_hba.conf as:
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
host all all 127.0.0.1/32 ident
host all all ::1/128 ident
local replication all peer
host replication all 127.0.0.1/32 ident
host replication all ::1/128 ident
With the above configuration, after ssh'ing onto the server, I can access the my_username database by executing psql and can also access the psql_user database by executing sudo -u psql_user psql and do not need to use a password for either.
But now, how to connect from the remote Windows PC?
To attempt to do so, I first created ssh keys without passphrases on the Windows PC for both my_username and psql_user and added the public key to each Linux user's authorized_keys (had to manually create /home/psql_user/ because it is a systems user). I can can successfully PuTTY to the server as either using the ssh keys.
Next, on the DBeaver connection settings SSH tab, I checked "Use SSH Tunnel", entered the username and private key location and the Test tunnel configuration successfully shows connected with the client version as SSH-2.0-JSCH-01.54 and server version as SSH-2.0-OpenSSH_7.4. I also made no changes to the Advanced portion of this tab such as local and remote hosts and ports, and have also left the "You can use variables in SSH parameters" at their default values.
Using my server IP in the main tab, Authentication "Database Native", and leave password empty, I test the connection but get The connection attempt failed. syslog reports that connection to the IP on port 5432 failed which makes sense because I am set up using Unix sockets.
So, then I change the server IP on the main tab to 127.0.0.1 (or localhost) and try again but get FATAL: Ident authentication failed for user "my_username". Okay, a little closer, but not quite there.
I think it might be because DBeaver is passing the port so I attempt to disable this part by got to the Edit Driver tab and changing jdbc:postgresql://{host}[:{port}]/[{database}] to jdbc:postgresql://{host}/[{database}], but now get Connection to 127.0.0.1:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Not sure where to go next. When I PuTTY into the Linux machine, all is good but not when connecting remotely using DBeaver, and thought it would be the same if I am using SSH to connect DBeaver to the server. How can this be accomplished?
As pointed out in the other answer, DBeaver's SSH tunnel option doesn't support sockets currently. It is always TCP port based, so only connections using the host options in pg_hba.conf can be made (I've placed a feature request for SSH socket forwarding in DBeaver).
Here's how to set up forwarding of a local TCP port to a remote Unix socket. This allows you to use peer authentication over the Unix socket, so you don't have to provide a password for the PostgreSQL role:
ssh username#dbserver.example.com -L 5555:/var/run/postgresql/.s.PGSQL.5432 -fN
While I think that ssh tunnelling can be set up to connect to a unix socket rather than a port, I don't think dbeaver offers a way to do that, so you would have to set it up separately.
Although ident should also work if your server runs the identd service. I think most linux don't do that by default, but just apt install oidentd or whatever the equiv would be on your package manager should fix that.
The easier solution would be to just change the method from ident to md5 or scram, and assign a password (which dbeaver offers to memorize).

Connect to remote postgres server from EC2 Instance

I am trying to fetch data from the Postgres server which is remotely available from Amazon EC2 instance. When I try to telnet the remote server, it is connected.
But when I am running a kafka connector which connects to the remote Postgres server it throws an error stating
FATAL: no pg_hba.conf entry for host, SSL off for configuration Couldn't open a connection to jdbc:postgresql://<url>
I tried changing the connection string from
jdbc:postgresql://host:5432/schema_name?user=******&password=******&defaultFetchSize=250000&useCursorFetch=true
to
jdbc:postgresql://host:5432/schema_name?ssl=true&user=******&password=******&defaultFetchSize=250000&useCursorFetch=true
then it throws another error which is
The server does not support SSL. for configuration Couldn't open a connection to jdbc:postgresql://<url>
There is no SSL support in the Postgres server because I can connect to the server through any DB connector without ssh. I am sure it has to do something with the security access group of EC2(considering I can telnet to the server from the instance). Any help would be much appreciated.
Looks like error is in the pg_hba.conf. I would have put this as a comment but not enough rep.
When you telnet to the server did you use the db port?
Can you post your pg_hba.conf file. This is one off my home dev server. You'll need to add a line similar to this: host all all 192.168.1.1/24 md5 with your IP addr and details.
If you're using this in a corporate network I'd highly recommend looking at a amazon VPC and not to expose your database to the internet.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 10.10.187.1/24 md5