Accessing postgresql server over network in Mac - macos

I have installed Postgres App on my Mac and have a database running.
I am able to connect it from the terminal by using the command psql -h localhost
Now I want to access this server from another machine which is on the same network.
When I do psql -h <hostname> -U <username> from the other machine, I get the error
psql: could not connect to server: Connection refused
Is the server running on host "my hostname" (xx.xx.xx.xxx) and accepting
TCP/IP connections on port 5432?
In the machine that runs the server I did lsof -i | grep LISTEN and I get the following result.
postgres 3196 sudarm 5u IPv6 0x1caf6120 0t0 TCP localhost:5432 (LISTEN)
postgres 3196 sudarm 6u IPv4 0x14678db0 0t0 TCP localhost:5432 (LISTEN)
postgres 3196 sudarm 7u IPv6 0x1caf6a80 0t0 TCP localhost:5432 (LISTEN)
Do I have to do anything else to connect to the server from another machine?

The error message asks the right question: is the server accepting connections on port 5432? The lsof output you provided indicates that no, it is not. PostgreSQL is listening on localhost:5432, meaning it will only accept connections from the database server itself.
Open the server's postgresql.conf, set listen_addresses = '*', and restart PostgreSQL. It will then be listening for connections over all interfaces, thus accepting connections over the network.
From here, the next problem you're likely to run into is authentication. (PostgreSQL will accept connections, but you probably haven't told it what to do once it has a connection from across the network.) Ensure the server's pg_hba.conf has an entry matching your database/user/source address combination -- something like host all all 10.0.0.0/8 md5 is probably appropriate -- and reload PostgreSQL as necessary to apply changes.
The md5 part tells PostgreSQL to attempt authentication using a password, so you will also need to set a password on the user in question if you don't have one set already. From however you normally administer your database (e.g. psql template1 on the database server), say ALTER USER whomever WITH PASSWORD 'your_new_password'.

Related

how do I connect to postgresql with psql from powershell?

I have installed PostgreSQL 13 on Windows 10 using the EDB installer and it seems to work fine. I can connect easily with psql in the included SQL Shell application. However, I can not connect from powershell:
~> psql -U postgres -h localhost
psql: error: 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?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Any permutation of psql command line switches yields the same result. I checked pg_hba.conf and it looks sane - all local connections are allowed. All the search results for the error code above on the internet assume the server is not running but it is running just fine (see SQL Shell above). Within that shell, all the postgres tables are visible, etc. What do I need to do to connect psql to the server on localhost from powershell?
Default installed pg_hba.conf:
# "local" is for Unix domain socket connections only
local all all scram-sha-256
# 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
The IPv4 line above should cover me in powershell? Yes? tcp connections are allowed in postgresql.conf:
listen_addresses = '*' # what IP address(es) to listen on;
Long time linux PostgreSQL user, first time in powershell on Windows, though...
This morning I found the problem - the EDB installer had setup the postgres service to run on port 5433. After a couple of decades or running it on 5432 I didn't even notice that the number was different in postgresql.conf. It was late...
Something else must be running on 5432. Will check later on.

Change Postgres defaut port in windows 10 console for psql command

When I install Postgres I make a mistake - change default port from 5432 to 5434 and after all, I change the port back through postgresql.conf. All app connections work fine with new 5432 port value, but cmd interface unavailable:
psql
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 5434?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5434?
I try to reinstall Postgres from scratch but without a result. How to change the port for windows cmd?
Clear the PGPORT environment variable, which must be set to 5434.
Then psql will use the default port 5432.

postgres: client could not connect to server

For clarification, please note that I have found several similar questions on stackoverflow, but I could not fix my issue.
psql: could not connect to server: Connection refused” Error when connecting to remote database
psql: How to Allow Remote Access to PostgreSQL database
I am trying to connect to a postgres DB installed in a remote server.
To create a connection from the client I am using QGIS plugin (Add PostGIS Layer):
The error that occurs is:
could not connect to server: Connection refused (0x0000274D/10061) is the server running on host "localhost" (::1) and accepting TCP/IP connection on port 5432
Additionally I added the host's IP Adress in the Host field, but then another error message occurs:
could not connect to server: Connection timed out (0x0000274C/10060) Is the server running on host "hosts_ip_adress" and accepting TCP/IP connections on port 5432?
My settings are:
Postgres installed version is 10.0 with PostGIS extension
Host operating System: Windows 7
Client operating System: Windows 7
I edited pg_hba.conf to:
host all all 0.0.0.0/0 md5
and changes listen parameter in 'postgresql.conf' to:
listen_addresses = '*'
both files are located in C:\Program Files\PostgreSQL\10\data
restartet pgAdmin4
My devices are in the same network. Each one is connected via LAN cable.
I suppose that a firewall is preventing the connection?

can't connect to postgres on windows10

I am setting up project on my windows PC and I have a problem with postgres.
Project is set on docker.
when I run docker-compose up I receive error
: *** Failed to connect to database dev; trying to create database
/usr/local/bundle/gems/sequel-4.48.0/lib/sequel/adapters/postgres.rb:224:in `initialize': PG::ConnectionBad: could not connect to server: Connection refused (Sequel::DatabaseConnectionError)
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
I don't know what to do. I have already installed microsoft easy-fix for TCP-IP, shut down windows firewall, changed all connection in pg_hba.conf to trust. Nothing helps. I don't have any antivirus software installed.
nmap report:
Nmap scan report for localhost (127.0.0.1)
Host is up (0.0027s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 995 closed ports
PORT STATE SERVICE
135/tcp open msrpc
445/tcp open microsoft-ds
2068/tcp open avocentkvm
2179/tcp open vmrdp
5432/tcp open postgresql
edit:
I run psql -h localhost and received
psql: CATASTROPHIC: role "Kamil" does not exist
Kamil is my PC name.
edit2:
is it possible that docker doesn't have access to postgres on localhost?
problem solved
I had to set environment var to DB_HOST=docker.for.win.localhost

Vagrant port fowarding on macOS?

I'm running postgresql on a Vagrant centos 7.2 box on macOS. I have confirmed postgres is up and running on the Vagrant box on port 5432 by connecting to it using psql on the box itself. I am attempting to forward port 5432 on the Vagrant box to port 10001 on my host machine as follows:
config.vm.define "acc_db" do | acc_db |
acc_db.vm.box = "bento/centos-7.2"
acc_db.vm.hostname = "acc.db"
acc_db.vm.network :forwarded_port, guest: 5432, host: 10001
acc_db.vm.provision "shell",
inline: "yum upgrade -y -q --nogpgcheck"
acc_db.vm.provision "shell",
path: "install_postgres.sh"
I have altered my pg_hba.conf to bind to all ip addresses and allow password authentication, as below:
# 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 md5
# IPv6 local connections:
host all all ::1/128 ident
I have turned off the firewall but I am still unable to connect to postgres on port 10001 on the host machine:
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.10001"?
How do I fix this so that port forwarding works?
I have seen https://gielberkers.com/fixing-vagrant-port-forwarding-osx-yosemite/ but haven't tried it as it involves touching files I'm unfamiliar with. Is it the correct approach? It looks like you have to explicitly allow every port you want to forward via Vagrant.
...connections on Unix domain socket "/tmp/.s.PGSQL.10001" means that you are not attempting a TCP/IP connection, so port forwarding cannot happen.
By default, psql use Unix domain sockets on unix-like OSes.
Use the -h option of psql to specify a host, presumably 127.0.0.1 if using IPv4.
Additionally, the PostgreSQL server must listen to the network interface to which the connection will be routed. By default, it listens only to localhost for security reasons.
Setting listen_addresses='*' in postgresql.conf makes it listen on all existing interfaces.

Resources