Connecting to a Postgresql database running on host OS - postgresql

i am running a postgresql instance in my host machine (Mac OS). I am able to access this instance by JDBC using localhost property in my Spring Boot application
Now i have a guest Ubuntu Virtual Box OS installed. and i want to install the same instance from this OS.
So i just changed the data source url of the same application from localhost to the host ip and port .
But now when i deploy the application and try to run it, i get "connection refused" error.
Telnet to host os from guest OS (10.0.2.2 5432) is working fine.
Content from my pg_hba.conf file
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres md5
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5
host all all 0.0.0.0/0 md5
host all all ::/0 md5
Connections and authentications content from my postgresql.conf
#------------------------------------------------------------------------------
# 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)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = '/tmp' # comma-separated list of directories
# (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)
So i have configured my postgrsql instance to accept connections from any IP.
i am behind a firewall.
Probably i am still missing somethings
Best Regards,
Saurav

This was a problem with my configuration of Spring Boot and Cloud Foundry..it was nowhere related to Postgresql.
Curious readers please head over to No unique service maching interface error in Spring Boot Cloud Foundry error for the solution

Related

How to handle SSL SYSCALL error connection when trying to connect to a remote PostgreSQL database?

I am trying to connect to a remote PostgreSQL database by using this command using my local terminal (Windows PowerShell):
psql -h 112.176.20.102 -p 5432 -d bookstore -U postgres -W
But it returns an error message as shown below:
psql: error: connection to server at "112.176.20.102", port 5432 failed: server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
SSL SYSCALL error: Connection reset by peer (0x00002746/10054)
connection to server at "112.176.20.102", port 5432 failed: expected authentication request from server, but received S
1. I have set up the pg_hba.conf configuration as shown below:
# PostgreSQL Client Authentication Configuration File
# ===================================================
...
# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local all postgres md5
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
host all all 0.0.0.0/0 md5
2. I also have set up postgresql.conf as shown below:
# -----------------------------
# PostgreSQL configuration file
# -----------------------------
...
#------------------------------------------------------------------------------
# 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)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories
...
My question is what causes this problem and how to handle it so that I can connect to my remote PostgreSQL database?

Not able to run postgres, even if `service postgresql status` says it's active

I have installed postgresql 13
with following conf
pg_hba.conf
# Database administrative login by Unix domain socket
local all postgres trust
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::0/00 trust
postgresql.conf
#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)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories
when I run service postgresql status, I get
when I do sudo su postgres and psql , I get

I am having trouble connecting my remote postgres service to pg_admin.Tried all possible way still failing

I want to connect my remote postgres service which is on a VM to my desktop pg_admin, but I am getting the same error again and again. I saw many people have the same issue and the solutions provided on stackoverflow is not helping me.
As of now these are the configurations I have made in postgres conf file and pg_hba.conf file.
pg_hba.conf :
# If you want to allow non-local connections, you need to add more
# "host" records. In that case you will also need to make PostgreSQL
# listen on a non-local interface via the listen_addresses
# configuration parameter, or via the -i or -h command line switches.
host replication replication master_ip/24 md5
host all all 0.0.0.0/0 md5
# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local all postgres trust
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all my_ip/32 md5
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
host all all my_ip/32 md5
host all all ::/0 md5
host all all 0.0.0.0/0 md5
postgresql.conf :
#------------------------------------------------------------------------------
# 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)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories
# (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)
pg_admin connection window :
Error that I get while connecting :
let me know what can be the possible issue for this, I have tried adding entry on pg_hba.conf and listen_port is allowd "*".
If any other info is needed please comment. There are many similar questions but that didn't help that's why adding the question.

Trying to allow postgresql to be accessed remotely always timeout expired

i tried to "enable" my postgresql13 to be accessed remotely.
So i tried to connect via pgadmin4 and it always said :
Unable to connect to server:
timeout expired
The ip when i check with command
ip a
it gave me
192.168.xxx.xxx/24
have tried both using with "/24" and without it
this is my pg_hba.conf
# 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 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
host all all all md5
this is my postgresql.conf
# - 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 = 5435 # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
#unix_socket_directories = '/var/run/postgresql, /tmp' # comma-separated list of directories
# (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)
I've already read from multiple sources here and there, change the listen addresses to become '*', and if wanted to change port, change it in the postgresql.conf port=desiredport
Am i missing something?
this is what i enter in the pgadmin4

can not connect to postgres from Docker

I have a docker image in which i have hadoop cluster composed fo one master and two slaves. On this cluster, I have HBase.
I'm trying to migrate my postgres Database which is installed on my local machine to HBase Database which is on the Docker image using scoop .
For postgres, this is my postgresql.conf configuration file:
#------------------------------------------------------------------------------
# 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)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart) unix_socket_directories = '/var/run/postgresql' # comma-separated list
of directories
# (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
# - Security and Authentication -
#authentication_timeout = 1min # 1s-600s ssl = on
#ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
#ssl_prefer_server_ciphers = on
#ssl_ecdh_curve = 'prime256v1'
#ssl_dh_params_file = '' ssl_cert_file = '/etc/ssl/certs/ssl-cert-snakeoil.pem' ssl_key_file =
'/etc/ssl/private/ssl-cert-snakeoil.key'
#ssl_ca_file = ''
#ssl_crl_file = ''
#password_encryption = md5 # md5 or scram-sha-256
#db_user_namespace = off
#row_security = on
# GSSAPI using Kerberos
#krb_server_keyfile = ''
#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
Also this is the content of 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 trust
# IPv4 local connections: host all all 127.0.0.1/32 trust
# IPv6 local connections: host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128
my problem is when I try to connect to postgres from docker image using scoop using this command:
sqoop import --connect jdbc:postgresql://localhost:5432/mimic --username postgres --password 0000 --table admission_ids --hbase-table mimic --column-family admission_ids --hbase-row-key id -m 1
I just have this problem:
Check that the hostname and port are correct and that the postmaster
is accepting TCP/IP connections. org.postgresql.util.PSQLException:
Connection refused. Check that the hostname and port are correct and
that the postmaster is accepting TCP/IP connections.
After a search, I jsut understand that docker is the origin of this issue, and to confirm that, I just try to connect to postgres using tenlnet from my local machine in the first time and this is the result :
telnet localhost 5432
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
and from the docker for the second time :
telnet localhost 5432
Trying 127.0.0.1...
Trying ::1...
telnet: Unable to connect to remote host: Cannot assign requested address
my docker image is named spark-hadoop.
I tried to trun this command but always not working:
docker run -d --name bridgeToHadoop --publish=127.0.0.1:5432:5432 -p 172.18.0.2:5432:5432 spark-hadoop
Postgresql running on host machine and you are trying to connect it from docker container. When you do this, host shouldn't be localhost in connection string. Here localhost means docker container.
Replace localhost with IP address, it should be following
sqoop import --connect jdbc:postgresql://host-pi-addr:5432/mimic --username postgres --password 0000 --table admission_ids --hbase-table mimic --column-family admission_ids --hbase-row-key id -m 1
Docker uses a default 172.17.0.0/16 subnet IP range for container networking. You need to allow non-local connections because your container network is no longer seen as local with respect to the postgres server. To permit external connections in the appropriate range add the following to pg_hba.conf (with appropriate choices for DATABASE, USER AND METHOD).
#TYPE DATABASE USER ADDRESS METHOD
host all all 172.17.0.0/16 scram-sha-256`
Also, by default the postgres server only listens on localhost, so you also need to tell postgres to listen on other IP addresses by adding the following to postgresql.conf.
listen_addresses = '*'