0

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.

3
  • If I revert the port back to the default ´5432´ it will work and I can connect. However, it isn't that secure to stick with default ports. Commented Nov 17, 2018 at 12:26
  • So, you prefer "security by obscurity"? Commented Nov 17, 2018 at 12:27
  • If you wanna say so, yes. To be fair - the port provided in the OP is just a dummy for explanation, because as a rule of thumb, you can trust nobody. But securing things does not only harden them on the one hand, it makes using them harder on the other hand. Commented Nov 17, 2018 at 12:29

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yup, that's a special thing for RHEL-based OSes, it say's that in the config file: ´Note: In RHEL/Fedora installations, you can't set the port number here; adjust it in the service file instead` Regarding security - you're probably right, but you should be able to change a port for whatever reason (e.g. another service already running on 5432).
So, managed to get it working if I specify the port in the command, like ´psql postgres -p 5633` when logged in as user postgres. Is there a way to set this to default? As you can see, I've already set PGPORT as an environment variable in the service config file (see OP).
You need to set it in the user's environment, not for systemd - ~/.bash_profile or similar.
Did that - works like a charm: I've edited ~/.bash_profile for user postgres and exported PGPORT to the port I wanted. Then, simply reloaded .bash_profile and everything is fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.