Can not connect to PostgreSQL listening on port 5432

  • 6,000
  • Tác giả: admin
  • Ngày đăng:
  • Lượt xem: 6
  • Tình trạng: Còn hàng

This issue comes from installing the postgres package without a version number. Although postgres will be installed and it will be the correct version, the script to lớn setup the cluster will not lập cập correctly; it's a packaging issue.

If you're comfortable with postgres there is a script you can lập cập to lớn create this cluster and get postgres running. However, there's an easier way.

First purge the old postgres install, which will remove everything of the old installation, including databases, so sánh back up your databases first.. The issue currently lies with 9.1 so sánh I will assume that's what you have installed

sudo apt-get remove --purge postgresql-9.1

Now simply reinstall

sudo apt-get install postgresql-9.1

Note the package name with the version number. HTH.

Thomas Ward

77.1k30 gold badges179 silver badges247 bronze badges

answered Aug 19, 2013 at 23:54

3

The error message refers to lớn a Unix-domain socket, so sánh you need to lớn tweak your netstat invocation to lớn not exclude them. So try it without the option -t:

netstat -nlp | grep 5432

I would guess that the server is actually listening on the socket /tmp/.s.PGSQL.5432 rather than vãn the /var/run/postgresql/.s.PGSQL.5432 that your client is attempting to lớn connect to lớn. This is a typical problem when using hand-compiled or third-party PostgreSQL packages on Debian or Ubuntu, because the source mặc định for the Unix-domain socket directory is /tmp but the Debian packaging changes it to lớn /var/run/postgresql.

Possible workarounds:

  • Use the clients supplied by your third-party package (call /opt/djangostack-1.3-0/postgresql/bin/psql). Possibly uninstall the Ubuntu-supplied packages altogether (might be difficult because of other reverse dependencies).
  • Fix the socket directory of the third-party package to lớn be compatible with Debian/Ubuntu.
  • Use -H localhost to lớn connect via TCP/IP instead.
  • Use -h /tmp or equivalent PGHOST setting to lớn point to lớn the right directory.
  • Don't use third-party packages.

answered Jun 27, 2011 at 17:41

3

This works for me:

Edit: postgresql.conf

sudo nano /etc/postgresql/9.3/main/postgresql.conf

Enable or add:

listen_addresses = '*'

Restart the database engine:

sudo service postgresql restart

Also, you can kiểm tra the tệp tin pg_hba.conf

sudo nano /etc/postgresql/9.3/main/pg_hba.conf

And add your network or host address:

host    all             all             192.168.1.0/24          md5

Zanna

71.6k60 gold badges222 silver badges330 bronze badges

answered Oct 9, năm trước at 13:17

7

Just create a softlink lượt thích this :

ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432

answered Jul 9, 2012 at 1:35

4

You can use psql -U postgres -h localhost to lớn force the connection to lớn happen over TCP instead of UNIX domain name sockets; your netstat output shows that the PostgreSQL server is listening on localhost's port 5432.

You can find out which local UNIX socket is used by the PostgrSQL server by using a different invocavtion of netstat:

netstat -lp --protocol=unix | grep postgres

At any rate, the interfaces on which the PostgreSQL server listens to lớn are configured in postgresql.conf.

answered Jun 26, 2011 at 12:51

I make it work by doing this:

dpkg-reconfigure locales

Choose your preferred locales then run

pg_createcluster 9.5 main --start

(9.5 is my version of postgresql)

/etc/init.d/postgresql start

and then it works!

sudo su - postgres
psql

Zanna

71.6k60 gold badges222 silver badges330 bronze badges

answered Sep 13, năm 2016 at 9:05

2

I had to lớn compile PostgreSQL 8.1 on Debian Squeeze because I am using Project Open, which is based on OpenACS and will not lập cập on more recent versions of PostgreSQL.

The mặc định compile configuration puts the unix_socket in /tmp, but Project Open, which relies on PostgreSQL, would not work because it looks for the unix_socket at /var/run/postgresql.

There is a setting in postgresql.conf to lớn mix the location of the socket. My problem was that either I could mix for /tmp and psql worked, but not project open, or I could mix it for /var/run/postgresql and psql would not work but project open did.

One resolution to lớn the issue is to lớn mix the socket for /var/run/postgresql and then lập cập psql, based on Peter's suggestion, as:

psql -h /var/run/postgresql

This runs locally using local permissions. The only drawback is that it is more typing than vãn simply "psql".

The other suggestion that someone made was to lớn create a symbolic links between the two locations. This also worked, but, the links disappeared upon reboot. It maybe easier to lớn just use the -h argument, however, I created the symbolic links from within the PostgreSQL script in /etc/init.d. I placed the symbolic links create command in the "start" section. Of course, when I issue a stop and start or restart command, it will try to lớn recreate an existing symbolic links, but other than vãn warning message, there is probably no harm in that.

In my case, instead of:

ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432

I have

ln -s /var/run/postgresql/.s.PGSQL.5432 /tmp/.s.PGSQL.5432

and have explicitly mix the unix_socket to lớn /var/run/postgresql/.s.PGSQL.5432 in postgresql.conf.

Peachy

7,17710 gold badges39 silver badges46 bronze badges

answered Nov 6, 2012 at 3:22

1

If your Postgres service is up and running without any error or there is no error in starting the Postgres service and still you are getting the mentioned error, follow these steps

Step1: Running pg_lsclusters will list all the postgres clusters running on your device

eg:

Ver Cluster Port Status Owner    Data directory               Log file
9.6 main    5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log

most probably the status will be down in your case and postgres service

Step 2: Restart the pg_ctlcluster

#format is pg_ctlcluster   
sudo pg_ctlcluster 9.6 main start

#restart postgresql service
sudo service postgresql restart

Step 3: Step 2 failed and threw error

If this process is not successful it will throw an error. You can see the error log on /var/log/postgresql/postgresql-9.6-main.log

My error was:

FATAL: could not access private key tệp tin "/etc/ssl/private/ssl-cert-snakeoil.key": Permission denied
Try adding `postgres` user to lớn the group `ssl-cert`

Step 4: kiểm tra ownership of postgres

Make sure that postgres is the owner of /var/lib/postgresql/version_no/main

If not, lập cập

sudo chown postgres -R /var/lib/postgresql/9.6/main/

Step 5: Check postgres user belongs to lớn ssl-cert user group

It turned out that I had erroneously removed the Postgres user from the ssl-cert group. Run the below code to lớn fix the user group issue and fix the permissions

#set user to lớn group back with
sudo gpasswd -a postgres ssl-cert

# Fix ownership and mode
sudo chown root:ssl-cert  /etc/ssl/private/ssl-cert-snakeoil.key
sudo chmod 740 /etc/ssl/private/ssl-cert-snakeoil.key

# now postgresql starts! (and install command doesn't fail anymore)
sudo service postgresql restart

answered Apr 16, 2018 at 14:44

1

I found uninstalling Postgres sounds unconvincing. This helps to lớn solve my problem:

  1. Start the postgres server:

    sudo systemctl start postgresql
    
  2. Make sure that the server starts on boot:

    sudo systemctl enable postgresql
    

Detail information can be found on DigitalOcean site Here.

answered Feb 1, 2018 at 15:45

Solution:

Do this

export LC_ALL="en_US.UTF-8"

and this. (9.3 is my current PostgreSQL version. Write your version!)

sudo pg_createcluster 9.3 main --start

answered Feb 23, năm 2016 at 21:47

1

In my case it was caused by a typo I made while editing /etc/postgresql/9.5/main/pg_hba.conf

I changed:

# Database administrative login by Unix domain name socket
local   all             postgres                                peer

to:

# Database administrative login by Unix domain name socket
local   all             postgres                                MD5

But MD5 had to lớn be lowercase md5:

# Database administrative login by Unix domain name socket
local   all             postgres                                md5

answered Nov 29, năm 2016 at 10:15

1

I failed to lớn solve this problem with my postgres-9.5 server. After 3 days of zero progress trying every permutation of fix on this and other sites I decided to lớn re-install the server and lose 5 days worth of work. But, I did replicate the issue on the new instance. This might provide some perspective on how to lớn fix it before you take the catastrophic approach I did.

First, disable all logging settings in postgresql.conf. This is the section:

# ERROR REPORTING AND LOGGING

Comment out everything in that section. Then restart the service.

When restarting, use /etc/init.d/postgresql start or restart I found it helpful to lớn be in superuser mode while restarting. I had a x-window open just for that operation. You can establish that superuser mode with sudo -i.

Verify that the server can be reached with this simple command: psql -l -U postgres

If that doesn't fix it, then consider this:

I was changing the ownership on many folders while trying to lớn find a solution. I knew that I'd probably be trying to lớn revert those thư mục ownerships and chmods for 2 more days. If you have already messed with those thư mục ownerships and don't want to lớn completely purge your server, then start tracking the settings for all impacted folders to lớn bring them back to lớn the original state. You might want to lớn try to lớn tự a parallel install on another system and systematically kiểm tra the ownership and settings of all folders. Tedious, but you may be able to lớn get access to lớn your data.

Once you tự gain access, systematically change each relevant line in the # ERROR REPORTING AND LOGGING section of the postgresql.conf tệp tin. Restart and test. I found that the mặc định thư mục for the logs was causing a failure. I specifically commented out log_directory. The mặc định thư mục the system drops the logs into is then /var/log/postgresql.

Zanna

71.6k60 gold badges222 silver badges330 bronze badges

answered Jan 19, 2017 at 1:34

Possibly it could have happened because you changed the permissions of the /var/lib/postgresql/9.3/main thư mục.

Try changing it to lớn 700 using the command below:

sudo chmod 700 main

Zanna

71.6k60 gold badges222 silver badges330 bronze badges

answered Nov 6, năm trước at 13:01

This is not exactly related to lớn the question since I'm using Flask, but this was the exact error I was getting and this was the most relevant thread to lớn get ideas.

My setup: Windows Subsystem for Linux, Docker-compose w/ makefile w/ dockerfile, Flask, Postgresql (using a schema consisting of tables)

To connect to lớn postgres, setup your connection string lượt thích this:

from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql+psycopg2://:@/"

NOTE: I never got any IP (e.g. localhost, 127.0.0.1) to lớn work using any method in this thread. Idea for using the container name instead of localhost came from here: https://github.com/docker-library/postgres/issues/297

Set your schema:

from sqlalchemy import MetaData
db = SQLAlchemy(app, metadata=MetaData(schema=""))

Set your tìm kiếm path for your functions when you setup your session:

db.session.execute("SET search_path TO ")

answered Mar 29, 2019 at 21:30

The most upvoted answer isn't even remotely correct because you can see in the question the server is running on the expected port (he shows this with netstat).

While the OP did not mark the other answer as chosen, they commented that the other answer (which makes sense and works) was sufficient,

But for these reasons that solution is poor and insecure even if it the server wasn't running on port 5432:


What you're doing here when you say --purge is you're deleting the configuration tệp tin for PostgreSQL ((as well as all of the data with the database. You or may not even see a warning about this, but here is the warning just to lớn show you now,

Removing the PostgreSQL server package will leave existing database clusters intact, i.e. their configuration, data, and log directories will not be removed. On purging the package, the directories can optionally be removed. Remove PostgreSQL directories when package is purged? [prompt for yes or no]

When you add it again PostgreSQL is reinstalling it to lớn a port number that's not taken (which may be the port number you expect). Before you even try this solution, you need to lớn answer a few questions along the same line:

  • Do I want multiple versions of PostgreSQL on my machine?
  • Do I want an older version of PostgreSQL?
  • What tự I want to lớn happen when I dist-upgrade and there is a newer version?

Currently when you dist-upgrade on Ubuntu (and any Debian variant), the new version of PostgreSQL is installed alongside the old copy and the port number on the new copy is the port number of the old copy + 1. So you can just start it up, increment the port number in your client and you've got a new install! And you have the old install to lớn fall back on -- it's safe!

However, if you only one want version of PostgreSQL purging to lớn change the configuration is still not the right option because it will destroy your database. The only time this could even be acceptable is you want to lớn destroy everything related to lớn PostgreSQL. You're better off ensuring your database is correct and then merely editing the configuration tệp tin so sánh the new install runs on the old port number

#!/bin/bash

# We can find the version number of the newest PostgreSQL with this
VERSION=$(dpkg-query -W -f "\${Version}" 'postgresql' | sed -e's/+.*//')
PGCONF="/etc/postgresql/${VERSION}/main/postgresql.conf"

# Then we can update the port.
sudo sed -ie '/port = /s/.*/port = 5432/' "$PGCONF"

sudo systemctl restart postgresql

Do not install a specific version of PostgreSQL. Only ever install postgresql. If you install a specific version then when you dist-upgrade your version will simply remain on your computer forever without upgrades. The repo will no longer have the security patches for the old version (which they don't support). This must always be suboptimal to lớn getting a newer version that they tự tư vấn, running on a different port number.

answered Aug 4, 2021 at 18:30

I had the exact same problem Peter Eisentraut described. Using the netstat -nlp | grep 5432 command, I could see the server was listening on socket /tmp/.s.PGSQL.5432.

To fix this, just edit your postgresql.conf tệp tin and change the following lines:

listen_addresses = '*'
unix_socket_directories = '/var/run/postgresql'

Now lập cập service postgresql-9.4 restart (Replace 9-4 with your version), and remote connections should be working now.

Now to lớn allow local connections, simply create a symbolic links to lớn the /var/run/postgresql directory.

ln -s /var/run/postgresql/.s.PGSQL.5432 /tmp/.s.PGSQL.5432

Don't forget to lớn make sure your pg_hba.conf is correctly configured too.

answered Nov trăng tròn, năm ngoái at 16:44

In my case, all i had to lớn tự was this:

sudo service postgresql restart

and then

sudo -u postgres psql

This worked just fine. Hope it helps. Cheers :) .

answered Jun 29, 2017 at 17:21

Find your file:

sudo find /tmp/ -name .s.PGSQL.5432

Result:

/tmp/.s.PGSQL.5432

Login as postgres user:

su postgres
psql -h /tmp/ yourdatabase

Zanna

71.6k60 gold badges222 silver badges330 bronze badges

answered Jan 30, 2017 at 15:18

I had the same problem (on Ubuntu 15.10 (wily)). sudo find / -name 'pg_hba.conf' -print or sudo find / -name 'postgresql.conf' -print turned up empty. Before that it seemed that multiple instances of postgresql were installed.

You might have similar when you see as installed, or dependency problems listing

.../postgresql
.../postgresql-9.x 

and so sánh on.

In that case you must sudo apt-get autoremove each package 1 by 1.

Then following this to lớn the letter and you will be fine. Especially when it comes to lớn key importing and adding to lớn source list FIRST

sudo apt-get update && sudo apt-get -y install python-software-properties && wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

If not using wily, replace wily with your release, i.e with the output of lsb_release -cs

sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ wily-pgdg main" >> /etc/apt/sources.list.d/postgresql.list'
sudo apt-get update && sudo apt-get install postgresql-9.3 pgadmin3

And then you should be fine and be able to lớn connect and create users.

Expected output:

Creating new cluster 9.3/main ...
config /etc/postgresql/9.3/main
data   /var/lib/postgresql/9.3/main
locale en_US.UTF-8
socket /var/run/postgresql
port   5432

Source of my solutions (credits)

Zanna

71.6k60 gold badges222 silver badges330 bronze badges

answered Feb 5, năm 2016 at 16:43

While having the same issue I tried something different:

Starting the postgresql daemon manually I got:

FATAL:  could not create shared memory segment ...
   To reduce the request size (currently 57237504 bytes), reduce PostgreSQL's
   shared memory usage, perhaps by reducing shared_buffers or max_connections.

So what I did was to lớn mix a lower limit for shared_buffers and max_connections into postgresql.conf and restart the service.

This fixed the problem!

Here's the full error log:

$ sudo service postgresql start
 * Starting PostgreSQL 9.1 database server                                                                                                                                                               * The PostgreSQL server failed to lớn start. Please kiểm tra the log output:
2013-06-26 15:05:11 CEST FATAL:  could not create shared memory segment: Invalid argument
2013-06-26 15:05:11 CEST DETAIL:  Failed system Call was shmget(key=5432001, size=57237504, 03600).
2013-06-26 15:05:11 CEST HINT:  This error usually means that PostgreSQL's request for a shared memory segment exceeded your kernel's SHMMAX parameter.  You can either reduce the request size or reconfigure the kernel with larger SHMMAX.  To reduce the request size (currently 57237504 bytes), reduce PostgreSQL's shared memory usage, perhaps by reducing shared_buffers or max_connections.
    If the request size is already small, it's possible that it is less than vãn your kernel's SHMMIN parameter, in which case raising the request size or reconfiguring SHMMIN is called for.
    The PostgreSQL documentation contains more information about shared memory configuration.

Zanna

71.6k60 gold badges222 silver badges330 bronze badges

answered Jun 26, 2013 at 13:29

After many exhausting attempts, I found the solution based on other posts!

dpkg -l | grep postgres
apt-get --purge remove  
whereis postgres
whereis postgresql
sudo rm -rf 
sudo userdel -f postgres

Kevin Bowen

19.8k57 gold badges80 silver badges83 bronze badges

answered Apr 22, 2019 at 23:46

1

  1. Check the status of postgresql:

    service postgresql status
    

    If it shows online, proceed to lớn step no 3 else execute step no 2.

  2. To make postgresql online, execute the following command:

    sudo service postgresql start
    

    Now kiểm tra the status by running the command of the previous step. It should show online.

  3. To start psql session, execute the following command:

    sudo su postreg
    
  4. Finally, kiểm tra if it's working or not by executing:

    psql
    

answered May 29, 2021 at 12:21

Restart postgresql by using the command

sudo /opt/bitnami/ctlscript.sh restart postgresql

answered Apr 26, 2022 at 9:41

This error could mean a lot of different things.

In my case, I was running PostgreSQL 12 on a virtual machine.

I had changed the shared_buffer config and apparently, the system administrator edited the memory config for the virtual machine reducing the RAM allocation from where it was to lớn below what I had mix for the shared_buffer.

I figured that out by looking at the log in

/var/log/postgresql/postgresql-12-main.log

and after that I restarted the service using

sudo systemctl restart postgresql.service

that's how it worked

answered Jun 30, 2022 at 9:47

Create postgresql directory inside lập cập and then lập cập the following command.

ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432

answered Apr 26, 2019 at 5:35

1

Simply add /tmp unix_socket_directories

postgresql.conf

unix_socket_directories = '/var/run/postgresql,/tmp'

answered Jun 17, 2019 at 1:00

2

I had this problem with another port. The problem was, that I had a system variable in /etc/environments with the following value:

PGPORT=54420

As I removed it (and restarted), psql was able to lớn connect.

answered Jul 18, 2021 at 14:46

2

You must log in to lớn answer this question.

Not the answer you're looking for? Browse other questions tagged

.