can not connect to postgres from Docker - postgresql

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 = '*'

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.

pgadmin4 unable to connect to server,FATAL: password authentication failed for user "postgres"

i tried to config postgresql to be able to connect to pgadmin4 "remotely"
So i installed postgresql version 13 in the server ,and i tried to access it via my laptop.
But why do i always get an error , i just changed the password and i enter the username and password in the pgadmin4 ,it still says password authentication.
image1
this is my pghba.conf file.
# 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
host all all 0.0.0.0/0 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 peer
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
this is my postgesql.conf file
listen_addresses = '*'
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5434 # (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)
# - TCP settings -
# see "man 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
#tcp_user_timeout = 0 # TCP_USER_TIMEOUT, in milliseconds;
# 0 selects the system default
# - Authentication -
#authentication_timeout = 1min # 1s-600s
password_encryption = scram-sha-256 # md5 or scram-sha-256
#db_user_namespace = off
# GSSAPI using Kerberos
#krb_server_keyfile = 'FILE:${sysconfdir}/krb5.keytab'
#krb_caseins_users = off
and still i don't know why, maybe someone experienced the same ? or have any idea?
fyi : i open the pgadmin4 application and the url going to this :http://127.0.0.1:60888/browser/
this is my pgadmin4 in browser
On image1 you changed the password for Postgres linux user, not for Postgres database user. Try next:
sudo -u postgres psql postgres # connect to Postgres
\password postgres # change password postgres user
\q # quite from Postgres shell
Then try to connect to Postgres again.
Would be helpful: https://docs.boundlessgeo.com/suite/1.1.1/dataadmin/pgGettingStarted/firstconnect.html
I solved it after changing in the pg_hba.conf
from
local all all trust
to become
host all all trust
Now it works like a charm.. Thankyou everyone! :)

Connecting to a Postgresql database running on host OS

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