Unable to make Remote Connection with Postgresql - database

I have PostgreSQL running on Ubuntu Server and I want to make remote connection with PostgreSQL running on port 5432.
I've checked if I can ping the public IP of ubuntu server from my machine and that works fine.
Next I've changed two files on ubuntu server first I've changed postgresql.conf which looks as below
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
Next I've added two lines in pg_hba.conf as below
host all all 0.0.0.0/0 trust
host all all ::/0 trust
Finally I checked if firewall is running by running sudo ufw verbose which outputted inactive.
As per my understanding I've allowed PostgreSQL to accept remote connection and firewall is also not present hence nothing is blocking. Still I get the following error.
psycopg2.OperationalError: connection to server at "XXX.XXX.XXX.XXX", port 5432 failed: Connection timed out (0x0000274C/10060)
Is the server running on that host and accepting TCP/IP connections?
How can I fix this error?
Edit
Although I can ping and ssh to the Ubuntu server using public IP but can not telnet.
I checked if port 5432 is open using this link but it turned out to be closed.

Related

Robot Framework: Connect to PostgreSQL Database via SSH Tunnel

i'm facing the following problem.
I've got a PostgreSQL database on a remote server. In order to access it, i need to pass through a SSH Tunnel.
So, the steps of my test are:
create the tunneling
Connect to the db
perform query / queries
I'm able to create the tunnel by using SSHLibrary, and it works:
SSHLibrary.Open Connection 10.xxx.xxx.xx
SSHLibrary.Login MySSH_User MySSH_Password delay=1
SSHLibrary.Create Local SSH Tunnel 9191 ${IP_DATABASE} 22
So i go on by using DatabaseLibrary to perform the connection (and i've checked all the fields are right):
DatabaseLibrary.Connect To Database psycopg2 ${DB_NAME} ${USER_DB} ${PASSWORD_DB} ${IP_DATABASE} ${PORT_DB}
but i get the following error:
OperationalError: could not connect to server: Connection timed out (0x0000274C/10060)
Is the server running on host "10.xxx.xxx.xx" and accepting
TCP/IP connections on port 5432?
How can i solve it? I think i need to specify in some way that the database connection must be done by using the tunnel but i don't know how to do it.
Here's the complete test code:
*** Settings ***
Library DatabaseLibrary
Library SSHLibrary
*** Variables ***
${IP_DATABASE} 10.xxx.xxx.xx
${PORT_DB} 5432
${DB_NAME} MyNameDB
${SCHEMA_DATABASE} MySchemaDB
${USER_DB} MyUserDB
${PASSWORD_DB} MyPasswordDB
*** Keywords ***
*** Test Cases ***
Connect To DB Via SSH
SSHLibrary.Open Connection 10.xxx.xxx.xx
SSHLibrary.Login MySSH_User MySSH_Password delay=1
SSHLibrary.Create Local SSH Tunnel 9191 ${IP_DATABASE} 22
Sleep 2s
DatabaseLibrary.Connect To Database psycopg2 ${DB_NAME} ${USER_DB} ${PASSWORD_DB} ${IP_DATABASE} ${PORT_DB}
I would like to do it by using RF, Anyone can help?
Thank's a lot
Ssh tunnels should work in a way that that you establish SSH connection to another host and expose a PORT in localhost that will then tunnel a traffic from that port in localhost to a IP/PORT in the end point of the tunnel ..
So, lets say you have a host at 192.168.100.10 where your robotframework is running and it needs to connect to a database in 192.168.50.100:
If the postgres is accessible via ssh directly:
Connect To DB Via SSH
${IP_DATABASE}= 192.168.50.100
${DB_PORT}= 5432
SSHLibrary.Open Connection ${IP_DATABASE}
SSHLibrary.Login MySSH_User MySSH_Password delay=1
SSHLibrary.Create Local SSH Tunnel ${DB_PORT} ${IP_DATABASE} ${DB_PORT}
DatabaseLibrary.Connect To Database psycopg2 ${DB_NAME} ${USER_DB} ${PASSWORD_DB} 127.0.0.1 ${PORT_DB}
Now, if you have to use ssh jump host, eg, connect to a ssh server that will then connect to another server where postgres is running, all you need to do is change the tunnel ip address where the traffic will be forward to:
Connect To DB Via SSH
${IP_DATABASE}= 192.168.50.100
${IP_SSH_JUMPHOST}= 192.168.50.1
${DB_PORT}= 5432
SSHLibrary.Open Connection ${IP_JUMPHOST}
SSHLibrary.Login MySSH_User MySSH_Password delay=1
SSHLibrary.Create Local SSH Tunnel ${DB_PORT} ${IP_DATABASE} ${DB_PORT}
DatabaseLibrary.Connect To Database psycopg2 ${DB_NAME} ${USER_DB} ${PASSWORD_DB} 127.0.0.1 ${PORT_DB}

Windows 10 Host cannot access SQL Server on Oracle Virtual Box

I have created a VM with virtual box that runs windows 10, and installed SQL Server.
Using Consume Adapter Service in Visual Studio for BizTalk 2020
Configured: SQLBinding AS: mssql://DESKTOP-T2G7B3A/MSSQL15.MSSQLSERVER/AFBizTalkTest?InboundId=ID
Click Connect:
Failure 26 - Error Locating Server/Instance Specified
Items tried or checked:
Ran PortQry on Virtual Box:
Starting portqry.exe -n 10.0.2.15 -e 1433 -p TCP ...
Querying target system called:
10.0.2.15
Attempting to resolve IP address to a name...
IP address resolved to DESKTOP-T2G7B3A
querying...
TCP port 1433 (ms-sql-s service): LISTENING
portqry.exe -n 10.0.2.15 -e 1433 -p TCP exits with return code 0x00000000.
1A. Ran PortQry On Host
Starting portqry.exe -n 10.0.2.15 -e 1433 -p TCP ...
Querying target system called:
10.0.2.15
Attempting to resolve IP address to a name...
Failed to resolve IP address to name
querying...
TCP port 1433 (ms-sql-s service): FILTERED
portqry.exe -n 10.0.2.15 -e 1433 -p TCP exits with return code 0x00000002.
1B. Verified the IP address on Virtual Box is 10.0.2.15. Failed to ping on Host, Successful ping on Virtual Box.
TCP/IP Enabled in SQL Server instance.
Changed Server name to ip address - Error Locating Server/Instance Specified
Verified SQL Server Browser is running.
Verified Server Name from Error Log.

postgres database not available over network

So I have changed the postgresql.conf file to this:
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
Then I changed the pg_hba.conf to host all all 127.0.0.1/32 and host all all 2601:643:8402:f900:1045:d5ae:1d3b:19d1
But I when I enter show listen_addresses; It still only lists local host. What do I need to fix in order to access my databases over a local network?
After changing those files you need to restart the database.

Issue using pg_dumpl

Ok, here's the issue: I want to upload a DB I've got locally and I'm using in localhost, but, when I use:
PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U root app_db > app_db.dump
I always get:
pg_dump: [archiver (db)] connection to database "app_db" failed: could not connect to server: Connection refused
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" and accepting
TCP/IP connections on port 5432?
I think the problem is that I'm writing localhost instead of my heroku app address, can anyone tell me how can I get the correct address?
You need to have postgresql installed and running

PSQLException when connecting to Postgres server via JDBC in same LAN (PGAdmin works)

I have some severe problems with connecting from DBVisualizer (8.0.9) to a PostgreSQL server which is running in the same LAN. DBVis is Java-based thus uses JDBC for connection. Connecting from PGAdmin works like a charm - only DBVis connection via JDBC isn't. And I need that to be solved!
Specs:
My PC: Ubuntu 12.04 LTS (64Bit), IP: 192.168.110.193
Server OS: Suse LINUX Enterprise Server 11, IP: 192.168.110.12
Postgresql server version: 9.1
Java VM: Java HotSpot(TM) 64-Bit Server VM
Java Version: 1.6.0_33
Java Vendor: Sun Microsystems Inc.
OS Name: Linux
OS Arch: amd64
OS Version: 3.2.0-25-generic
When starting a connection, I'm getting a "Connecting..." message and after ~5 minutes of waiting the following error message appears in the connection window:
"An error occurred while establishing the connection:
Long Message:
The connection attempt failed.
Details:
Type: org.postgresql.util.PSQLException
SQL State: 08001"
In the debug console I get:
12:04:57 [DEBUG pool-2-thread-8 D.ā] RootConnection: Driver.acceptsURL("jdbc:postgresql://192.168.110.12:5432/MYDATABASE")
12:04:57 [DEBUG pool-2-thread-8 D.ā] RootConnection: Driver.connect("jdbc:postgresql://192.168.110.12:5432/MYDATABASE", {user=******, password=******})
12:24:58 [DEBUG pool-2-thread-8 D.ā] RootConnection: EXCEPTION -> org.postgresql.util.PSQLException: The connection attempt failed.
The debugging information of the JDBC driver is also provided:
org.postgresql.util.PSQLException: The connection attempt failed.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:150)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:66)
at org.postgresql.jdbc2.AbstractJdbc2Connection.(AbstractJdbc2Connection.java:125)
at org.postgresql.jdbc3.AbstractJdbc3Connection.(AbstractJdbc3Connection.java:30)
at org.postgresql.jdbc3g.AbstractJdbc3gConnection.(AbstractJdbc3gConnection.java:22)
at org.postgresql.jdbc4.AbstractJdbc4Connection.(AbstractJdbc4Connection.java:30)
at org.postgresql.jdbc4.Jdbc4Connection.(Jdbc4Connection.java:24)
at org.postgresql.Driver.makeConnection(Driver.java:393)
at org.postgresql.Driver.connect(Driver.java:267)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.onseven.dbvis.d.B.D.ā(Z:1548)
at com.onseven.dbvis.d.B.F$A.call(Z:278)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:679)
For convenience the relevant part of the server's pg_hba.conf:
#"local" is for Unix domain socket connections only
local all all peer
#IPv4 local connections:
host all all 192.168.110.0/24 md5
#IPv6 local connections:
host all all ::1/128 md5
And the relevant parts of the postgresql.conf:
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost', '*' = all
# (change requires restart)
#port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
# Note: Increasing max_connections costs ~400 bytes of shared memory per
# connection slot, plus lock space (see max_locks_per_transaction).
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directory = '' # (change requires restart)
#unix_socket_group = '' # (change requires restart)
#unix_socket_permissions = 0777 # begin with 0 to use octal notation
# (change requires restart)
#bonjour = off # advertise server via Bonjour
# (change requires restart)
#bonjour_name = '' # defaults to the computer name
# (change requires restart)
# - Security and Authentication -
#authentication_timeout = 1min # 1s-600s
#ssl = off # (change requires restart)
#ssl_ciphers = 'ALL:!ADH:!LOW:!EXP:!MD5:#STRENGTH' # allowed SSL ciphers
# (change requires restart)
#ssl_renegotiation_limit = 512MB # amount of data between renegotiations
#password_encryption = on
#db_user_namespace = off
# Kerberos and GSSAPI
#krb_server_keyfile = ''
#krb_srvname = 'postgres' # (Kerberos only)
#krb_caseins_users = off
# - TCP Keepalives -
# see "man 7 tcp" for details
#tcp_keepalives_idle = 0 # TCP_KEEPIDLE, in seconds;
# 0 selects the system default
#tcp_keepalives_interval = 0 # TCP_KEEPINTVL, in seconds;
# 0 selects the system default
#tcp_keepalives_count = 0 # TCP_KEEPCNT;
# 0 selects the system default
The connection is running now but unfortunately I cannot exactly tell the steps that led to the solution. Anyway, I will try to sketch them:
I installed an old version of DBVisualizer (7.1.5) and was able to successfully establish a connection to the db server. Then I went straight back to the 8.0.9
version of DBVis and tested the connection again. Unexpectedly, the connection also worked here, even though I didn't change configurations - neither in my DBVis 8.0.9 installation nor on the DB server. That's it. Maybe someone has some some more clues on that issue.
The error code 08001 is a generic error code telling that the JDBC driver could not connect to the database. The reason for this could be many.
You should enter the IP address or DNS name of the server where the database runs as the Database Server and the Database Port it listens to for TCP/IP connections, by default 5432.
After you have entered this, please use the Ping Server button to see if you can reach that server and port. If you get an error message, you have either entered incorrect Database Server or Port values, there is a firewall that blocks the connection, or the PostgreSQL server is not configured to access connections from your PC. If Ping Server says everything is fine but you still cannot connect, the problem is most likely with the login credentials for the user account you specify.

Resources