Unable to access PostgreSQL running in Linux Ubuntu on top of Hyper-V in Windows 10 - postgresql

I got this confusing error of unable to access PostgreSQL running inside a Linux on top of Hyper-V on Windows 10.
Below is part of my hb_pga.conf. I already check it using psql show hba_file that I edited the correct configuration file.
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 md5
If I add listen_addresses='*', I can't access it from within linux VM itself (nor Windows host). But If I remove listen_addresses, and test using IP connection via psql -h 127.0.0.1 -U postgres postgres, it will work.
I am using default switch and add another internal switch for samba mounted drive. Not sure whether that means something though.
Using this command to check all opened ports:
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1368/postgres
Shouldn't 127.0.0 be 0.0.0.0 to allow connecting from outside?

Solved.
Turn out, that listen_address='*' argument is on different file on the same directory with pg_hba.conf, that is postgresql.conf
Adding listen_addresses='*' to postgresql.conf solved this issue.
I put all of it here, should you need it https://medium.com/#swdev/quick-start-postgresql-on-linux-windows-and-hyper-v-bf5eef40eb84

Related

Postgres remote connection - Windows server

I have the following lines in pg_hba.conf. Postgres is installed on a Windows server.
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 md5
#host all all myip md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
host replication all 127.0.0.1/32 md5
host replication all ::1/128 md5
I would like to allow connection only from one more public IP address. How can I achieve that? As soon as I enable the above line IP: Postgres doesn't start.
Looking for some guidance.
To open the port 5432 edit your /Program Files/PostgreSQL/10/data/postgresql.conf and change
# Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
In /Program Files/PostgreSQL/10/data/pg_hba.conf
# IPv4 local connections:
host all all 0.0.0.0/0 md5
Now restate the Postgres server use cmd
pg_ctl -D "C:\Program Files\PostgreSQL\10\data" restart
I run postgres on several Windows servers (Windows Server 2012 R2, Windows Server 2016) as a part of a commercial framework running on an Apache tomcat webserver. The local connection worked fine. However, I want the same postgres server to be accessible for another framework (Cakephp) on another server (Red Hat Enterprise Linux) on the same server farm. This used to work until I upgraded to postgres 9. Now I had to upgrade to postgres 10. No matter what I tried, I failed.
Here is what I did to solve the problem:
Find your local postgres configuration files. They are usually in the same directory as the postgres tablespace, in my case:
d:\PG10Data\postgresql.conf.
This file has to contain the following lines:
# - Connection Settings:
listen_addresses = '*' # what IP addresses/interfaces to listen on
port = 5432
The next file to be modified is the pg_hba.conf (hba = host based access):
# TYPE DATABASE USER ADDRESS METHOD
# some sample entries:
# this will open UP ALL REMOTE IPv4 connections, do not open up permanently
host all all 0.0.0.0/0 md5
# only 1 database for 1 user from 1 IPv4 address:
host yourdatabasename yourusername 10.15.17.13/32 md5
# don not forget the "/32", otherwise the postgres server will not start up!
After editing these files, restart the postgres server.
You can run
netstat -a -n | findstr 5432
to see if the postgres listener is running.
You can also run the following to test connectivity from the Windows command prompt:
psql -Uyourusername -dyourdatabasename -p5432 -hlocalhost
This should work at any time. The next level will be to use your computer's local IPv4 address. This you can find out with
ipconfig
This will tell you your local IPv4 address. Use this in the following command:
psql -Uyourusername -dyourdatabasename -p5432 -hyourlocalip
My problem was, this command failed. Since I ran it directly on my server, it could not be the local Windows firewall.
The solution:
There is a 2nd configuration file:
d:\PG10Data\postgresql.auto.conf
The file starts ominously with the following 2 lines:
# Do not edit this file manually!
# It will be overwritten by the ALTER SYSTEM command
And it ends:
listen_addresses = 'localhost'
I tried the ALTER SYSTEM command when I was logged on locally as postgres via psql, but no success.
In the end I turned cheeky and changed the entry in d:\PG10Data\postgresql.auto.conf to;
listen_addresses = '*'
Bingo! After a postgres restart, remote access worked like a charm on both Windows servers.
N. B. Don't forget the Windows Firewall: Open port 5432 for remote access. Also check there are no firewalls in the network the block access to port 5432 from your remote client.
It would be nice if someone were able to tell me how I should have changed the parameter without editing the postgresql.auto.conf, but at least both of my framesworks - both on the local and the remote server - are working.
That is incorrect syntax: myip is not an IP address, and the /32 after it is missing.

Cannot connect to postgresql remotely

I cannot connect to my postgresql instance remotely on port 5432. Connection times out. I cannot connect remotely using psql, pgAdmin or telnet. I feel like I have everything configured correctly. Postgresql has been restarted several times with these settings. Remote connection works fine.
What could I be missing?
netstat:
postgressql.conf
pg_hba.conf:
Try referring this link for help
https://www.thegeekstuff.com/2014/02/enable-remote-postgresql-connection/?utm_source=tuicool
in short you have to the following steps
1. Add the following line to the pg_hba.conf server. This will allow connection from “192.168.xxx.xx” ip-address (This is the client ip)
# vi /var/lib/pgsql/data/pg_hba.conf
host all all 192.168.xxx.xx/xx trust
Change the Listen Address in postgresql.conf
# grep listen /var/lib/pgsql/data/postgresql.conf
listen_addresses = '*'
Test the Remote Connection
You just need to change the method md5 to trust and and enter the address as 0.0.0.0/0.
Open the config file
vi /var/lib/pgsql/data/pg_hba.conf
Insert the following line
host all all 0.0.0.0/0 trust
Maybe it will work.

Can't connect to postgreSQL in VM (Ubuntu): Access to database denied

I use a Windows 10 machine with Virtualbox 5.0.16 in which Ubuntu 15.10 is running. The network adapter is a Bridged one with the name set to Intel(R) Dual Band Wireless-AS 3165. The promiscuous-Mode is set to allow all VMs and the host and the underlying checkbox is checked (cable connected).
In this Ubuntu, I run a PostgreSQL 9.4.5 instance in which I configured both the postgresql.conf and pg_hba.conf files.
In the postgresql.conf file, I have well set this line:
listen_addresses = '*'
And in the pg_hba.conf, I have following lines at the bottom:
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 md5
host all all 0.0.0.0/0 md5
host all all ::1/128 md5
host all all ::/0 md5
host all all 192.168.1.13 md5
host all all 0.0.0.0/0 md5
Currently, the ip address of my host machine is (ipconfig command and wireless) 192.168.1.13 and the guest machine is (ifconfig command) 192.168.1.4
When using pgadmin from the host machine to connect to the guest machine, I get following message: FATAL: no pg_hba.conf entry for host "192.168.1.13", user "postgres", database "postgres", SSL off
I tried to connect to my host to guest via ssh but I get a "Network error: Connection refused" message. Maybe this is a key insight of my issue?
The ping works in both directions.
Thanks to #DanielVérité.
The command show hba_file showed me the pg_hba I was actually editing was not the right one. I edited the one in /etc/postgresql/9.4/main folder while the one used by postgreSQL is /opt/PostgreSQL/9.4/data

"psql: could not connect to server: Connection refused" Error when connecting to remote database

I am trying to connect to a Postgres database installed in a remote server using the following command:
psql -h host_ip -U db_username -d db_name
This is the error that occurs:
psql: could not connect to server: Connection refused
Is the server running on host "<host_ip>" and accepting
TCP/IP connections on port 5432?
Postgres installed version is 9.4.
Host operating system: Ubuntu 15.04
Client operating system: Centos 7
I already tried the following but the issue remains unresolved:
Edited pg_hba.conf file to include
host all all 0.0.0.0/0 md5
Edited 'postgresql.conf' and changed the listen parameter to
listen_addresses='*'
Restarted Postgres service.
Disabled firewall and iptables on host and client.
I checked by running the psql command locally and it worked.
I tried the second solution given in this question. Running nmap gave me the following output:
Starting Nmap 6.47 ( http://nmap.org ) at 2015-09-07 18:08 IST Nmap scan report for 10.17.250.250 Host is up (0.0000040s latency). Not shown: 997 closed ports PORT STATE SERVICE 22/tcp open ssh 25/tcp open smtp 80/tcp open http
Am I missing something? Hope someone can help.
cd /etc/postgresql/9.x/main/
open file named postgresql.conf
sudo vi postgresql.conf
add this line to that file
listen_addresses = '*'
then open file named pg_hba.conf
sudo vi pg_hba.conf
and add this line to that file
host all all 0.0.0.0/0 md5
It allows access to all databases for all users with an encrypted password
restart your server
sudo /etc/init.d/postgresql restart
Check the port defined in postgresql.conf. My installation of postgres 9.4 uses port 5433 instead of 5432
I have struggled with this when trying to remotely connect to a new PostgreSQL installation on my Raspberry Pi. Here's the full breakdown of what I did to resolve this issue:
First, open the PostgreSQL configuration file and make sure that the service is going to listen outside of localhost.
sudo [editor] /etc/postgresql/[version]/main/postgresql.conf
I used nano, but you can use the editor of your choice, and while I have version 9.1 installed, that directory will be for whichever version you have installed.
Search down to the section titled 'Connections and Authentication'. The first setting should be 'listen_addresses', and might look like this:
#listen_addresses = 'localhost' # what IP address(es) to listen on;
The comments to the right give good instructions on how to change this field, and using the suggested '*' for all will work well.
Please note that this field is commented out with #. Per the comments, it will default to 'localhost', so just changing the value to '*' isn't enough, you also need to uncomment the setting by removing the leading #.
It should now look like this:
listen_addresses = '*' # what IP address(es) to listen on;
You can also check the next setting, 'port', to make sure that you're connecting correctly. 5432 is the default, and is the port that psql will try to connect to if you don't specify one.
Save and close the file, then open the Client Authentication config file, which is in the same directory:
sudo [editor] /etc/postgresql/[version]/main/pg_hba.conf
I recommend reading the file if you want to restrict access, but for basic open connections you'll jump to the bottom of the file and add a line like this:
host all all all md5
You can press tab instead of space to line the fields up with the existing columns if you like.
Personally, I instead added a row that looked like this:
host [database_name] pi 192.168.1.0/24 md5
This restricts the connection to just the one user and just the one database on the local area network subnet.
Once you've saved changes to the file you will need to restart the service to implement the changes.
sudo service postgresql restart
Now you can check to make sure that the service is openly listening on the correct port by using the following command:
sudo netstat -ltpn
If you don't run it as elevated (using sudo) it doesn't tell you the names of the processes listening on those ports.
One of the processes should be Postgres, and the Local Address should be open (0.0.0.0) and not restricted to local traffic only (127.0.0.1). If it isn't open, then you'll need to double check your config files and restart the service. You can again confirm that the service is listening on the correct port (default is 5432, but your configuration could be different).
Finally you'll be able to successfully connect from a remote computer using the command:
psql -h [server ip address] -p [port number, optional if 5432] -U [postgres user name] [database name]
Make sure the settings are applied correctly in the config file.
vim /etc/postgresql/x.x/main/postgresql.conf
Try the following to see the logs and find your problem.
tail /var/log/postgresql/postgresql-x.x-main.log
Following configuration, you need to set:
To open the port 5432 edit your /etc/postgresql/9.1/main/postgresql.conf and change
# Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
In /etc/postgresql/10/main/pg_hba.conf
# IPv4 local connections:
host all all 0.0.0.0/0 md5
Now restart your DBMS
sudo service postgresql restart
Now you can connect with
psql -h hostname(IP) -p port -U username -d database
Step 1: edit file potgresql.conf
file location should be : etc/postgresql/10/main/
Look for:
#Connection Settings -
#listen_addresses = '' # what IP address(es) to listen on;
remove # before listening addresses
add '*' :
listen_addresses = '*'
Step 2: edit file pg_hba.conf
file location should be : etc/postgresql/10/main/
add below given line at the end
host all all 0.0.0.0/0 md5
Step 3: restart postgres server
sudo /etc/init.d/postgresql restart
Step 4: check postgres server status
sudo netstat -plunt |grep postgres
Make sure you are using same port to access the DB
Mine was quite straightforward if you are on a Mac try:
brew install postgres
This will tell you if you have it already install and what version or install the latest version for you if not then run
brew upgrade postgresql
This will make sure you have the latest version installed then finally
brew services start postgresql
This will start the service again. I hope this helps someone.
I think you are using the machine-name instead of the ip of the host.
I got the same error when i tried with machine's name. Because, It is allowed only when both the client and host are under same network and they have the same Operating system installed.
In my case, I did not change azure default security policy in management portal. The original is port 22 allowed and the rest are all denied. As long as I add 5432 port, everything becomes good.
The following helped me on macos Mojave:
$sudo mv /usr/local/var/postgres /usr/local/var/postgres.save
$brew uninstall postgres
$brew install postgres
See the port and make a port change in postgresql.conf. My installation of postgres 9.4 uses port 5431 or 5434 instead of 5432.
If it say the port is in use so change the port.
And check if you give password in psql installation so give the password in file and save it.
In my case I had removed a locale and generated another locale. Database failed to open because of fatal errors in the postgresql.conf file, on 'lc_messages', 'lc_monetary', 'lc_numberic', and 'lc_time'.
Restoring the locale sorted it out for me.
Another situation,postgresql.confandpg_hba.conffile not locate at /etc/postgresql/9.1/main/.Because postgres can start at any location you set.
For example when you use command pg_ctl -D /tmp/pgsql/ start ,the postgresql.conf and pg_hba.conf will located at /tmp/pgsql/.
I had the exact same problem, with my configuration files correct. In my case the issue comes from the Eduroam wifi I used : when I connect via another wifi everything works. It seems that Eduroam blocks port 5432, at least in my university.
Try to migrate your database. For instance, if you are using Heroku to host your project and with Django, then try heroku run python manage.py migrate command; the error should go away.
I had a problem like this where I had to ssh into a server and than run a query in psql console so the query was in a script but everytime I got this error psql not found so what I did was just added the psql full path from the bin which we get from cat .bash_profile and its done
PATH=$PATH:/usr/local/pgsql/bin:/usr/local/mysql/bin
So I added the whole /usr/local/mysql/bin/psql intead of just psql for remote execution.
and another one here:
both host and remote are on real servers
you need '*' exactly.
'localhost , xxx.xxx.xxx.xxx' will not work. all these answers(i've seen two) should be wiped out.
what you don't need : host all all 0.0.0.0/0 md5 and this stuff
For me, I just removed the existing PostgreSQL 14 Server which was on the left-hand side of the pgAdmin4 GUI interface under the servers and then I manually added a new server from the option which is inside Quick Links of pgAdmin4.
I followed the documentation of bitnami.com.
I had a problem with access to external server via 5432.
I noticed that any network but mine saw the service
nmap server -p 5432
Fortunatelly, I recalled that I was playing with exposing my internal postgres server to outside world using my mikrotik router.
Somehow it effectively closed external 5432 for internal network.
As soon as I removed all nat rules with 5432 port - it worked like a charm.

psql cant connect to PostgreSQL server (postmaster) on IP and port 5432?

Please read before replying it as duplicate (as it perhaps can happen). I am running my postmaster (postgres) server. See below for 'sudo netstat -anp|grep 5432' output?
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 29606/postmaster
unix 2 [ ACC ] STREAM LISTENING 1650581 29606/postmaster /var/run/postgresql/.s.PGSQL.5432
unix 2 [ ACC ] STREAM LISTENING 1650582 29606/postmaster /tmp/.s.PGSQL.5432
I am able to connect from localhost using
psql -h localhost (OR 127.0.0.1) -d <DB> -U user -W
But when I try to connect from other hosts using tcp, by specifying
psql -h ip_add_postmaster -d <DB> -U user -W
It throws:
psql: could not connect to server: Connection refused
Is the server running on host XXXXXX and accepting TCP/IP connections on port 5432?
What's wrong here?
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 md5
# IPv6 local connections:
host all all ::1/128 md5
In postgresql.conf,
listen_addresses = 'localhost, 127.0.0.1, ip_add_postmaster'
Note: ip_add_postmaster is same as my Elastic IP and not public DNS. If this information
matters.
What am I doing wrong here? Machine is hosted on Amazon EC2 and have open the port 5432.
As your netstat output indicates, it's listening at 127.0.0.1:5432 which is localhost. That is only connectable from localhost ;)
Set listen_addresses='*' in your config and it will work.
[edit]
Other things to check:
is the amazon firewall blocking anything?
is iptables blocking anything?
But first make sure the listening address is correct, your netstat output shows that it won't work like this.
listen_addresses='localhost, private_ip' fixed the issue. I was not able to start postmaster server on elastic IPs. Once postgres server started o localhost and private IPs, I was able to connect.
One other issue I have found was if you end up with two Postgres installations, the second one can choose non-default port (in my case it was 5433 i/o 5432). So checking the port in postgresql.conf might be a good idea.
I ran into this issue and tried all sorts of fixes I found across SO, and want to add a simple solution that worked for me after realizing it had to do with permissions in my case.
Simply, if you're running a psql server on Windows, you are initially restricted to the default postgres superuser for logging in, launching the server, and so on.
So, first try running from the command line:
psql -U postgres -h localhost -p 5432
and enter your password at the prompt. If you've managed to login and the server is up, then it was a permissions issues. From here, you can create a role for yourself that has login privileges to whatever database you are trying to run.
If the error persists, then consider checking postgresql.conf as mentioned above, to make sure default IP is set to * or localhost, and the port set to 5432 or whatever port you want as default.
I also ran into the same issue. On debugging, it was nothing related to the port, but due to some missing directories in the Postgres folder.
While updating Mac OS (from 10.13.1 -> 10.13.13), some folders in the directory /usr/local/var/postgres/ gets deleted. The fix was the adding the missing directories:
mkdir /usr/local/var/postgres/pg_tblspc
mkdir /usr/local/var/postgres/pg_twophase
mkdir /usr/local/var/postgres/pg_stat
mkdir /usr/local/var/postgres/pg_stat_tmp
mkdir /usr/local/var/postgres/pg_replslot
mkdir /usr/local/var/postgres/pg_snapshots
mkdir /usr/local/var/postgres/pg_logical/{snapshots,mappings}