Cannot connect PostgreSQL with psql - wrong UNIX-Domain-Socket - postgresql

I'm running CentOS 7.5 and cannot setup PostgreSQL.
If I'm logged in as user postgres and type psql postgres I get the following error message:
psql: Could not connect to server: no such file or directory
does the server run locally and accepts connections on Unix-Domain-Socket "/var/run/postgresql/.s.PGSQL.5432"
However, I changed the port to 5543 (did so in etc/systemd/system/postgresql.service by including /lib/systemd/system/postgresql.service as a [Service] and setting Environment=PGPORT=5543). Note that you shouldn't change it directly in /lib/ because that will get overwritten.
So, the server looks for the wrong UNIX-Domain-Socket and does not find one (because it does not exist), but the socket for the correct port 5543 does exist according to sudo netstat -nlp:
5486/postgres /var/run/postgresql/.s.PGSQL.5543
postgresql.service is running according to systemctl status postgresql.service
Any constructive help is appreciated.

Why are you changing it in systemd and not using PostgreSQL's config file? Is that a CentOS thing?
Anyway - you can run the server on any port you like, or run multiple server instances of the same or different versions on a variety of ports. In that case though you need to tell psql what port to use.
You can set an environment variable (PGPORT), specify it with -p on the command-line or in a .psqlrc file. See the manuals for details.
Edit in response to comments:
If you want to set the PGPORT for psql, do it in the user's shell defaults or in /etc/bash... or equivalent. You could of course replace psql with an alias using your custom port or recompile the binary itself if you wanted.
I'm not sure this is really much use from a security perspective. It seems unlikely that someone can run local processes on your machine, has gained access to your postgres user password but isn't smart enough to see what port the server is running on.

Related

PostgresSQL is not getting connected

I am new to Ubuntu operating system and trying to install postgresSQL in my system.
After completing the installation and entering postgres mode when I enter the command pgsql, it gives me this error
psql: error: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
what could possibly be wrong??
Probably the postgresql server is not running; you can run netstat -adp to see if postgres is listening on port 5432.
If it isn't, then it most likely isn't running; systemctl status postgresql will help identify if it is even installed, and if it has run into any issues (for further details, see https://mydbanotebook.org/post/troubleshooting-01/)
If it is running and seems healthy, then it might not be configured to accept connections in whichever way you are connecting (which user, database, host/network etc) - see details about the pg_hba.conf file that includes rules for this - https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html
After install, you must create an instance with initdb.
Later, change the listen_address to *, restart the service and now you can check the port.
Check the pg log for the message: "postgres is ready to accept connections"

Postgres service started, but can't connect. Nothing listening on port 5432

I don't know how this happened, but my postgres server (9.6.2) has stopped working locally. I seem to be able to start it up, and stop it using:
brew services start postgresql and brew services start postgresql but when I try to connect using psql I get this error:
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.5432"?
When I try to see if anything is listening on port 5432 using lsof -i :5432 I get nothing.
Looked at other posts that say check the pg_hba.conf file, which I did, but doesn't seem to be the problem.
I ended up reinstalling postgres via homebrew. That by itself didn't do enough. I had to kill my data directory to get this to work (i.e. rm -rf /path/to/my/data/directory) and then rebuild it using initdb.
This solution obviously only works if you don't care about keeping your data (in my case, it was just data for my dev environment, so I didn't). If you do want to save a copy of your data, I you'd have to come up with something else.

Openbravo - Connecting pgadmin with postgresql database in Ubuntu

I have installed Openbravo ERP 3.0 in Ubuntu 12.04(LTS). After completed the installation I tried to connect with postgresql database using pgadmin 9.1. I gave all the details as mentioned in this link: http://wiki.openbravo.com/wiki/Installation/Appliance/Openbravo.
Also I have changed the config file settings like listen_addresses="*". I got this error after I click "Ok" button
Could anyone give suggestion to fix this error? Thanks in advance.
Actually the problem comes because of not mentioning the local cluster or data area for PostgreSQL. To do this after installing Openbravo, need to give this command in terminal
psql -d openbravo -U tad -h localhost -p 5932
Finally give the PostgreSQL configuration details as mentioned in this link
It works fine now...
To be able to reach the server remotely you have to add the following line into the file: /var/lib/pgsql/data/postgresql.conf:
listen_addresses = '*'
PostgreSQL, by default, refuses all connections it receives from any remote address. You have to relax these rules by adding this line to /var/lib/pgsql/data/pg_hba.conf:
host all all 0.0.0.0/0 md5
This is an access control rule that lets anyone login from any address if a valid password is provided (the md5 keyword). You can use your network/mask instead of 0.0.0.0/0 to only allow access from certain IP addresses.
When you have applied these modifications to your configuration files, you will need to restart the PostgreSQL server.
/etc/init.d/postgresql start
Now, you will be able to login to your server remotely

How to connect to postgres with two terminals?

I have a terminal I can connect to postgres
psql testdb
but when I open a new tab or a new terminal and tried to connect to psql an error is returned
This error returns
psql: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5433"?
I tried creating another user and using another port
createuser testing_multiple
psql -p 5433
the same error occurs.
Your issue is probably that you're working within virtualenv. It isn't a chroot, but it sets a bunch of environment variables to fake the local environment, quite likely including the PATH and other settings.
Most likely the unix socket is at a different location to that compiled into the psql binary you're using. I'd say your PostgreSQL server socket is probably actually at /tmp/.s.PGSQL.5433, in which case export PGHOST=/tmp/ will work. Since you can connect from within the virtualenv terminal you can check though - within psql, run SHOW unix_socket_directories;. The location shown there is what you can give in PGHOST to connect to that PostgreSQL server.
The reason that export PGHOST=localhost works is that you are forcing psql (and other clients that use libpq) to connect over TCP/IP, instead of the default unix socket connection.

pgadmin gives me the error: no password supplied

I've installed postgresql 9.2 on linux (kubuntu) and the last version of pgadmin3, but when I connect them I have this error:
An error has occurred:
Error connecting to the server: fe_sendauth: no password supplied
What can I do?
I have also configured tomcat for my web application in java. In fact, postgresql was working before trying my application.
Change the password for role postgres:
sudo -u postgres psql postgres
alter user postgres with password 'postgres';
Try connect using "postgres" for both username and password.
Refer to: How to change PostgreSQL user password
Whether a password is required depends on your settings in pg_hba.conf. And there are different ways you can connect - different settings in pg_hba.conf may apply.
I quote the help shipped with pgAdmin 3 for the "Host" field in the connection ("server") settings:
The host is the IP address of the machine to contact, or the fully
qualified domain name. On Unix based systems, the address field may be
left blank to use the default PostgreSQL Unix Domain Socket on the
local machine, or be set to an alternate path containing a PostgreSQL
socket. If a path is entered, it must begin with a “/”. The port
number may also be specified.
If you connect via Unix socket the rules for "local" apply.
Whereas when connecting via TCP/IP "host" (or "hostssl") rules applies.
If you have a line like this at the top your pg_hba.conf file:
local all all peer
or:
local all all ident
.. then you can connect locally without password if your system user is "postgres" and your database user is "postgres", too.
I realize this is question is years old, but I ran into this same problem today and have a solution that uses trust in a limited but useful way.
As in many development shops, when the devs need a QA postgres password, they just yell it, message it, email it, write it on their foreheads, etc. And I'm like, "This is really bad. I need to figure out a way to use PKI here." We also use pgAdmin3.
First, add a line like this to your pg_hba.conf, where dev represents the user for the developers in your shop:
host all dev 127.0.0.1/32 trust
Drop the developers' public key in their authorized_keys folder on the database server. Now have them ssh into the server with the -L flag with a command similar to the following:
ssh -i ~/.ssh/id_rsa -L5432:127.0.0.1:5432 -vvv 101.102.103.104
This allows one to use the postgres port as if it were localhost. Of course, replace the key, server and make sure to map to an open port locally (if you have a local postgres running, it's probably bound to 5432). I use a pretty verbose flag so I can easily troubleshoot any ssh issues.
Open another terminal and issue this command:
psql -h 127.0.0.1 -U dev -p 5432
You should have access to the database and never be prompted for a password, which I think is great because otherwise, the devs will just waive the password around with little regard to security, passing it out like Halloween candy.
As of now, PgAdmin3 will still prompt you for a password, even though -- plain as day -- you do not need it. But other postgres GUIs will not. Try Postico. It's in beta but works great.
I hope this answer helps anyone like me who would rather use PKI for postgres auth rather than sharing passwords willy-nilly.
Met this problem recently.
If you're using PostgreSQL on local machine, and psql works well without logging needed, try pgadmin3's menu File - Add Server - Properties tab, fill in Name field for this connection, leave Host field and Password field empty, and click ok.
from pgadmin docs
On Unix based systems, the address field may be left blank to use the
default PostgreSQL Unix Domain Socket on the local machine, or be set
to an alternate path containing a PostgreSQL socket. If a path is
entered, it must begin with a “/”.
Worked on Debian testing (pgadmin3 1.22, PostgreSQL 11), without touching pg_hba.conf.
For me, I run pg_ctl -D /usr/local/var/postgres start, start the server, then everything is OK, it will pop out the connection host port.