Cannot allow external connections in Postgres - postgresql

I have a server with Postgres installed and I want to allow the connection from external IP's. Postgres works very well for local connections, then at the end of the file /var/lib/pgsql/data/postgressql.conf I added:
listen_addresses = '*'
And in the end of /var/pgsql/data/pg_hba.conf I added:
host all all 0.0.0.0/0 md5
host all all ::/0 md5
Then I restarted postgres with: sudo systemctl restart postgresql
And tried to connect with: psql -h my.db.server.ip -U postgres
And I get:
psql: could not connect to server: No existe ninguna ruta hasta el «host»
Is the server running on host "my.db.server.ip and accepting
TCP/IP connections on port 5432?
I am using Centos 7 and PostgreSQL 9.2.23
What else should I do?

Use
netstat -an | grep 5432
on the server to see if anything is listening on port 5432.
See what port is set to in postgresql.conf.
Use lsof on the PostgreSQL server process to see what sockets PostgreSQL is listening on.

Related

Connect to existing instance of Postgresql from WSL 2

I'm running Ubuntu WSL 2 on Windows 10. Before I installed WSL I already had Postgresql installed on my Windows 10. I wanted to connect to the database from WSL but so far it fails.
When running:
psql
I get:
psql: error: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
When running:
psql -h 127.0.0.1 -p 5432 -U postgres
I get:
psql: error: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
And WSL clearly doesn't see the database. When running this:
sudo service postgresql start
It returns:
postgresql: unrecognized service
I know that one solution is to install the database in WSL but wanted to try to connect to existing instance first.
You should edit pg_hba.conf
check wsl subnet
execute ipconfig from cmd. Below is example.
Ethernet Adapter vEthernet (WSL)
IPv4 Address . . . . . . . . . . . .: 172.19.230.81
edit pg_hba.conf
pg_hba.conf path is C:\Program Files\PostgreSQL\{postgresqlVersion}\data\pg_hba.conf. if you installed Windows Postgresql default install path.
subnet mask is use value from ipconfig
host all all 172.19.230.0/24 md5
restart postgresql
you can restart postgresql from services.msc. service name is postgresql-x64-{postgresqlVersion}.
access from psql
psql -h 172.19.230.81 -p 5432 -U postgres

Unable to connect to server pgadmin4

When attempting to add a server (this is my first install)
Configuration:
postgres 12
centos 7
pgadmin 4
I get
could not connect to server: Permission denied
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Permission denied
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
per
SHOW listen_addresses;
listen_addresses
------------------
localhost
I can network connect from psql
psql -h localhost aps19
psql (12.1, server 9.2.24)
Type "help" for help.
aps19=>
I am listening on port 5432
I am able to make TCP connections from java from my localhost
postgres>psql
psql (12.1, server 9.2.24)
Type "help" for help.
xxx=> show port
xxx-> ;
port
------
5432
per How connect Postgres to localhost server using pgAdmin on Ubuntu?
psql (12.1, server 9.2.24)
Type "help" for help.
postgres=#
postgres=# alter user postgres with password 'postgres';
ALTER ROLE
postgres=#
also
netstat -a | grep 5432
unix 2 [ ACC ] STREAM LISTENING 30611 /tmp/.s.PGSQL.5432
unix 2 [ ACC ] STREAM LISTENING 30602 /var/run/postgresql/.s.PGSQL.5432
use the "ifconfig" cmd to find real docker ip which name is dock0.
And then use it as username to login.

Heroku Postgresql pg:psql No Route to host

For now I am just trying to make the heroku pg:psql command work but my final purpose is to copy a database that I have on my computer (localhost) to the heroku postgresql database with the pg:push command.
For now when I simply try to access the database that I created on heroku, the heroku pg:psql command returns:
psql: could not connect to server: No route to host
Is the server running on host "ec*-**-***-***-**.eu-west-1.compute.amazonaws.com" (**.***.***.**) and accepting
TCP/IP connections on port 5432?
postgresql.conf: (the lines are not commented)
listen_address ='*'
port = 5432
ssl = true
and host all all **.***.***.** trust in pg_hba.conf
I also tried to add rules to iptables in order to give access to the database from the host IP address provided by heroku.
I am on a Debian computer, how can I solve this?
psql: could not connect to server: No route to host
It means your PostgreSQL server is not starting up or is starting up on a different port.
Solutions you may try:
Check PostgreSQL service by command ps -ef | grep Postgres.
Check the port which PostgreSQL is listening to by command netstat -tupln | grep Postgres.
Make sure your server enables UDP port because PostgreSQL needs UDP port loopback for stats collector service.
Check the startup logs or database logs at pg_log about the problem.

Amazon EC2: PostgreSQL remote connection hangs

I'm trying to set up PostgreSQL remote connection on Ubuntu server.
I changed listen_addresses = 'localhost' to listen_addresses = '*' in /etc/postgresql/9.3/main/postgresql.conf
Then I added to postgre config file /etc/postgresql/9.3/main/pg_hba.conf lines
host all all 0.0.0.0/0 md5
host all all ::0/0 md5
host all all all md5
But when I'm trying to connect remote DB by psql -U db_user -h host -d db_name it hangs and show message
psql: could not connect to server: Connection timed out
Is the server running on host "host_address" and accepting
TCP/IP connections on port 5432?
PgAdmin connection hangs as well
Had the same issue using amazon EC2 service
Solution:
1) Go to security group for your server instance
2) Edit inbound rules
3) Allow all traffic for your IP

Not able to load raster to postgis

I want to load a raster file in postgis
In command prompt I'm running
C:\Program Files\PostgreSQL\9.4\bin>raster2pgsql -I -C -s 4326 D:\Aparna\Data\PNV\pnv_new.tiff public.testraster|psql -d geodjango
I get the following error
ERROR: Unable to read raster file: D:\Aparna\Data\PNV\pnv_new.tiff
psql: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
received invalid response to SSL negotiation: -
My port is 5433. After going through few posts I changed the line in pg_hba.conf
from
# IPv4 local connections:
host all all 127.0.0.1/32 md5
to
# IPv4 local connections:
host all all 0.0.0.0/0 trust
but its not working. How can I solve this?
If you are not running postgres on port 5432, you need to add a -p <port> to the command, i.e. -p 5433, but according to the error message, you seem to have problems "at both ends" - both with connecting to the server and with reading the file.