When changing the default port number from 5432 to 5332, why do I get a unix domain socket error?

All we need is an easy explanation of the problem, so here it is.

I successfully followed the Postgres 11 installation instructions on Centos 7 found here. I am able to successfully create schemas / tables and insert data into the database over port 5432 with no problems.

Now I would like to change the default port to 5332 (or something else, doesn’t matter), and I did the following:

 vi /var/lib/pgsql/11/data/postgresql.conf

Changed the line:

#port = 5432                            # (change requires restart) 

to look like:

port = 5332                            # (change requires restart) 

Then restarted postgres service by doing:

systemctl restart postgresql-11.service

Then I change to the postgres user:

su - postgres

I’m getting the following error message:

sql: 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"?

So apparently there is another reference of port 5432 somewhere, but I am unable to find it.

FYI, if I undo the change I did above (in the file /var/lib/pgsql/11/data/postgresql.conf), then everything works perfectly again over default port 5432.

What else do I need to do to get postgres to run on a different port ?

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You may have changed the port for the PostgreSQL database, but not the client.

Try this:

  1. Update your PostgreSQL configuration to use port 5332 again
  2. Test this by passing the port number when trying to connect:
    psql -p 5332
  3. If everything works, change the default port for the command-line client like this:
    PGPORT=5332; export PGPORT

Now all calls to psql will act as if it has been invoked with the -p 5332 command-line option.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply