Postgres connection issue - postgresql

I installed and started my Postgres database with brew (on my Mac). I also defined an entry in my /etc/hosts file (I tried both with 127.0.0.1 postgres and with postgres).
However, when I try
psql -h postgres -U postgres -p 5432
I cannot connect
psql: could not connect to server: Connection refused.
However, when I try with
psql -h localhost -U postgres -p 5432
I can connect. What is needed to be able to connect with: psql -h postgres -U postgres -p 5432

Make sure your PostgreSQL server is willing to accept tcp/ip connections on port 5432.
In your PostgreSQL configuration file check these values.
listen_addresses = '*'
port = 5432

Related

why am I getting Postgres password authentication fails error?

I'm trying to learn and exercise with postgres.
I'm running on Ubuntu 16.04.
I installed Postgres docker and run it with the following command:
sudo docker run --rm --name pg-docker -e POSTGRES_PASSWORD=docker -d -p 5433:5433 -v $HOME/amitliron/UserA/docker/volumes/postgres:/var/lib/postgresql/data postgres
(I'm using 5433 port, becuse when trying to use 5432 port I'm getting the following error:
Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use, I think it's not need to be problem, but writing here everying I did)
I'm trying to check the postgres docker with the following command:
psql -h localhost -U postgres -d postgres
and entered the password: "docker" but I'm getting the following error:
Password for user postgres:
psql: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
What am I missing ?
I needed to specify the port:
psql -h localhost -p 5433 -U postgres -d postgres

Problem with postgresql and pgadmin docker containers

I'm trying to connect postgresql and pgadmin4 work together.
pgadmin4 works fine but when I try to create a new server I have 2 problems:
if the postgres container is at other port that is not 5432 it dont recognize that port. It show this error: could not connect to server: Connection refused
Is the server running on host "172.17.0.5" and accepting
TCP/IP connections on port 5431?
if the postgres container is at port 5432 the error is FATAL: password authentication failed for user "example".
I execute this command to get postgres container: docker run -p 5431:5432 --name postgres2 -e POSTGRES_PASSWORD=ad1234 -d postgres.
I try, following other responses in stackoverflow, adding this command -c"listen_addresses='*'" and I enter in the config file too but noone of this work to me.
Hope you can help me, thanks.
EDIT [Solved]
Ok I solved, it was a big fail by my part. I was using 172.17.0.5 (the IP container address) and what I need to use to connect is 172.17.01 (the Gateway).
Thanks for you time.
I have reproduce your scenario this way:
# docker run -p 5431:5432 --name postgres2 -e POSTGRES_PASSWORD=ad1234 -d postgres
# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d4030c577a24 postgres "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 0.0.0.0:5431->5432/tcp postgres2
# sudo -u postgres psql -h localhost -p 5431
could not change directory to "/root": Permission denied
Password:
psql (10.5, server 11.2 (Debian 11.2-1.pgdg90+1))
WARNING: psql major version 10, server major version 11.
Some psql features might not work.
Type "help" for help.
postgres=# CREATE DATABASE mytestdb;
CREATE DATABASE
postgres=# \q
Now starting docker for pgadmin and being able to connect to postgresql:
docker run -p 80:80 --link postgres2 -e "PGADMIN_DEFAULT_EMAIL=user#domain.com" -e "PGADMIN_DEFAULT_PASSWORD=SuperSecret" -d dpage/pgadmin4
With the above command you can link the postgres2 docker to the pgadmin docker and then on creating a connection on pgadmin4 you should use:
host name/address: postgres2
port: 5432
Maintenance database: postgres
username: postgres
with that, I've connected to Postgres from pgadmin4
As far as I know, docker PostgreSQL comes by default with localhost only connection and if you want to add remote connection you should add "listen_addresses = '*'" to postgresql.conf

Cannot migrate postgres db between 2 servers ubuntu

I'm trying to migrate data from old server to new server, both of them used ubuntu server.
I have used this command:
pg_dump -C -h localhost -U localuser dbname | psql -h remotehost -U remoteuser dbname
but it throw:
psql: could not connect to server: Connection timed out
Is the server running on host "X.X.X.X" and accepting
TCP/IP connections on port 5432?
I have checked postgresql conf on new server, running on 5432, listen..="*"
Anything wrong?
Thanks all !

PostgreSQL: Remotely connecting to Postgres instance using psql command

I want to remotely connect to a Postgres instance. I know we can do this using the psql command passing the hostname
I tried the following:
psql -U postgres -p 5432 -h hostname
I modified the /etc/postgresql/9.3/main/pg_hba.conf file on the target machine to allow remote connections by default
I added the following line to the file
host all all source_ip/32 trust
I restarted the cluster using
pg_ctlcluster 9.2 mycluster stop
pg_ctlcluster 9.2 mycluster start
However, when I try to connect from the source_ip, I still get the error
Is the server running on host "" and accepting TCP/IP connections on port 5432?
What am I doing wrong here?
I resolved this issue using below options:
Whitelist your DB host from your network team to make sure you have access to remote host
Install postgreSQL version 4 or above
Run below command:
psql -h <REMOTE HOST> -p <REMOTE PORT> -U <DB_USER> <DB_NAME>
psql -h <IP_Address> -p <port_no> -d <database_name> -U <DB_username> -W
-W option will prompt for password. For example:
psql -h 192.168.1.50 -p 5432 -d testdb -U testuser -W
I figured it out.
Had to set listen_addresses='*' in postgresql.conf to allow for incoming connections from any ip / all ip
Step Wise below
Opening the Port - Make sure the PSQL Port is open to all remote connections or connections from a specific set of IPs as per your requirement. PSQL, in general, runs at port 5432, and it is configurable, so expose relevant Port accordingly.
Update Remote Server PSQL Configuration - Set listen_addresses = '*' in postgresql.conf file, path in general is /etc/postgresql/psql_version/main/postgresql.conf
Connect remotely - psql -U <db_username> -h <IP_address> - in case psql is running on a port other than 5432 on the remote server, specify port by adding -p <port_number>
A little plus below -
In case the IP has been mapped to a domain name, you can connect by replacing <IP_address> with <host_name>. To do this, add a new connection rule in pg_hba.conf file
Note -
All above explained can cause security issues - best practice always is to either keep your psql port closed, or only allow a list of IPs to connect through the port.
Note that "ident" in pg_hba.conf requires a "ident server" to be running on the client.

pgAdmin3 backup over ssh tunnel

I have a running postgresql server on amazone ec2. I connect with pgAdmin3 to it over ssh tunnel directly configured in pgAdmin3 from my mac.
I can make queries and see the full schema, no problem about that.
If I try to make a backup of the database (from pgAdmin3 GUI) then I get (even if the connection is actually open and working) the following exception:
/Applications/pgAdmin3.app/Contents/SharedSupport/pg_dump --host localhost --port 5432 --username "MY_USERNAME" --role "MY_ROLE" --no-password --format custom --encoding UTF8 --verbose --file "/Users/XXX/filename" "DATABASENAME"
pg_dump: [archiver (db)] connection to database "DATABASENAME" failed: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Process ended with Exitcode 1.
Any idea why pg_dump in the background can not connect over the ssh tunnel?
alternative until i found a solution is to make it by terminal
ssh <HOST> "pg_dump -U <USERNAME> -W -h localhost -F c <DATABASENAME> | gzip -c" > ./backup.sql.gz
This line worked for me:
ssh -o "Compression=no" server_adress "pg_dump -Z9 -Fc -U postgres db_name" > backup_name.dump