Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
Asked Answered
C

29

147

Before anything, please note that I have found several similar questions on Stack Overflow and articles all over the web, but none of those helped me fix my issue:

Now, here is the issue:

  • I have a Rails app that works like a charm.
  • With my collaborator, we use GitHub to work together.
  • We have a master and an mvp branches.
  • I recently updated my git version with Homebrew (Mac).
  • We use Foreman to start our app locally.

Now, when I try to launch the app locally, I get the following error:

PG::ConnectionBad at /
could not connect to server: Connection refused
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?

I tried to reboot my computers several times.

I also checked the content of /usr/local/var/postgres:

PG_VERSION      pg_dynshmem     pg_multixact    pg_snapshots    pg_tblspc       postgresql.conf
base            pg_hba.conf     pg_notify       pg_stat         pg_twophase     postmaster.opts
global          pg_ident.conf   pg_replslot     pg_stat_tmp     pg_xlog         server.log
pg_clog         pg_logical      pg_serial       pg_subtrans     postgresql.auto.conf

As you can see, there is no postmaster.pid file in there.

Any idea how I could fix this?

Comorin answered 18/5, 2016 at 18:20 Comment(3)
Can you update the question with the content of postgresql.conf?Katanga
I know this is a very old question. But today I faced the same problem in Mac and brew reinstall postgresql@13 resolved the problem for me. (It is a requirement to use v13 for my project)Phillipphillipe
Follow this: https://mcmap.net/q/48935/-django-connection-to-postgres-by-docker-composeSyneresis
C
76

You most likely ran out of battery and your postgresql server didn't shutdown correctly.

The easiest workaround is to download the official postgresql app and launch it: it will force the server to start (http://postgresapp.com/)

Cleodal answered 18/5, 2016 at 18:25 Comment(8)
Thanks for your answer. I just downloaded the app and launched it. If I shut it down before launching my server, then I still get the same error. If I don't shut it down, then I get an error message stating that my "database does not exist". Can this be related?Comorin
If your database does not exist, you should run rake db:create . But this is most likely not related ... Did you just fork a repo or something like this ? You shouldn't shut it down before launching your server using 'rails s' !! The app has to be running the whole time you want to access your site on a localhostCleodal
Ok. No, I did not fork a repo, it is the same repo I was working with. But I ran git fetch, maybe this is the problem.Comorin
git fetch is not supposed to be deleting the database. What's happening if you run rake db:create ?Cleodal
"Ran out of battery"... this is exactly what happened! :-OThing
With this error the other client application 'pgAdmin'did not run the server nor used the 5432 port. But fixed it by reinstalling PostgreSQL DBMS.Bloody
It just started doing this when I downloaded it the very first time. Your solution tho is 100% working and fixed itDuvall
thank you so much. after so much search this ended up fixing my issueOldtimer
A
193

run postgres -D /usr/local/var/postgres and you should see something like:

 FATAL:  lock file "postmaster.pid" already exists
 HINT:   Is another postmaster (PID 379) running in data directory "/usr/local/var/postgres"?

Then run kill -9 PID in HINT

And you should be good to go.

Anthraquinone answered 27/7, 2017 at 13:41 Comment(9)
Best solution for my case, which is that my machine shut down unexpectedly and restarting the service was not solving the issue - thanks!Ganny
If you're using homebrew, brew services start postgres or brew services restart postgres might do the trickInformality
thank you! I ran with this issue for the second time, how did you understand that this would be the solution?Bladdernose
Any equivalent solution for linux ubuntu dist?Cumulous
This worked for me when nothing else on the internet did. My machine did shut down unexpectedly. Thanks so much.Florescence
Best solution for my case, which is that my machine shut down unexpectedly and restarting the service was not solving the issue - thanks!Northwestwards
We are in dec 2020 and this solution work for me after a long time of research. thanks a lotHebel
No such file or directoryEustatius
Where do I locate this file on windows?Twophase
W
77

Most likely it's because your system shutdown unexpectedly

Try

postgres -D /usr/local/var/postgres

You might see

FATAL:  lock file "postmaster.pid" already exists
HINT:  Is another postmaster (PID 449) running in data directory "/usr/local/var/postgres"?

Then try

kill -9 PID

example

kill -9 419

And it should start postgres normally

Weaver answered 15/11, 2017 at 17:41 Comment(1)
Yes my system shut down unexpectedly and this solved my problem. thank you.Noctambulous
C
76

You most likely ran out of battery and your postgresql server didn't shutdown correctly.

The easiest workaround is to download the official postgresql app and launch it: it will force the server to start (http://postgresapp.com/)

Cleodal answered 18/5, 2016 at 18:25 Comment(8)
Thanks for your answer. I just downloaded the app and launched it. If I shut it down before launching my server, then I still get the same error. If I don't shut it down, then I get an error message stating that my "database does not exist". Can this be related?Comorin
If your database does not exist, you should run rake db:create . But this is most likely not related ... Did you just fork a repo or something like this ? You shouldn't shut it down before launching your server using 'rails s' !! The app has to be running the whole time you want to access your site on a localhostCleodal
Ok. No, I did not fork a repo, it is the same repo I was working with. But I ran git fetch, maybe this is the problem.Comorin
git fetch is not supposed to be deleting the database. What's happening if you run rake db:create ?Cleodal
"Ran out of battery"... this is exactly what happened! :-OThing
With this error the other client application 'pgAdmin'did not run the server nor used the 5432 port. But fixed it by reinstalling PostgreSQL DBMS.Bloody
It just started doing this when I downloaded it the very first time. Your solution tho is 100% working and fixed itDuvall
thank you so much. after so much search this ended up fixing my issueOldtimer
R
41

The postgresql server might be down and the solution might be as simple as running:

sudo service postgresql start

which fixed the issue for me.

Ranger answered 13/3, 2017 at 18:29 Comment(0)
N
32

This could be caused by the pid file created for postgres which has not been deleted due to unexpected shutdown. To fix this, remove this pid file.

  1. Find the postgres data directory. On a MAC using homebrew it is /usr/local/var/postgres/, other systems it might be /usr/var/postgres/

  2. Remove pid file by running:

    rm postmaster.pid
    
  3. Restart postgress. On Mac, run:

    brew services restart postgresql
    
Nunes answered 29/6, 2018 at 6:12 Comment(2)
Thx! Personally I had to remove in this way: rm /Users/<username>/Library/Application\ Support/Postgres/var-<versionId>/postmaster.pidRomantic
What is the cause of this? I never had these types of issues with MySql?Dhoti
B
18

I had almost just as same error with my Ruby on Rails application running postgresql(mac). This worked for me:

brew services restart postgresql
Bombardon answered 16/8, 2019 at 9:27 Comment(0)
C
16

This worked in my case:

brew uninstall postgresql
rm -fr /usr/local/var/postgres/
brew install postgresql
Chrisoula answered 2/5, 2017 at 3:1 Comment(1)
these installation paths are darn confusing. Finally, this one worked. Removing the previous installation files was the solution.Holcman
K
8
  1. In my case PostgreSQL updates from version 13.4 to 14 in background, so it fixes by:

    brew postgresql-upgrade-database
    
  2. In other case the problem fixed by:

    rm /usr/local/var/postgres/postmaster.pid
    

    or

    rm /opt/homebrew/var/postgres/postmaster.pid
    

    or by version (for example postgresql@14)

    rm /opt/homebrew/var/postgresql@14/postmaster.pid
    
  3. Restart service postgresql:

    brew services restart postgresql
    

PS:

How can you understand what is the problem?

For first see what service is not correct started:

brew services list

For second show file postgres.log, where will be the error:

tail -f /usr/local/var/log/postgres.log

or

tail -f /opt/homebrew/var/log/postgres*

And so find answer by this error's text

Kermis answered 20/10, 2021 at 15:38 Comment(0)
E
6

Step 1:

cd /etc/postgresql/12/main/

open file named postgresql.conf

sudo nano postgresql.conf

add this line to that file

listen_addresses = '*'

then open file named pg_hba.conf

sudo nano pg_hba.conf

and add this line to that file

host  all  all 0.0.0.0/0 md5

It allows access to all databases for all users with an encrypted password

restart your server

sudo /etc/init.d/postgresql restart
Elyssa answered 3/9, 2020 at 14:22 Comment(0)
W
4

I resolved the issue via this command

pg_ctl -D /usr/local/var/postgres start

At times, you might get this error

pg_ctl: another server might be running; trying to start server anyway

So, try running the following command and then run the first command given above.

pg_ctl -D /usr/local/var/postgres stop
Winebaum answered 10/7, 2018 at 6:49 Comment(0)
D
4

I encountered a similar problem when I was trying to connect my Django application to PostgreSQL database.

I wrote my Dockerfile with instructions to setup the Django project followed by instructions to install PostgreSQL and run Django server in my docker-compose.yml.

I defined two services in my docker-compose-yml.

services:
  postgres:
    image: "postgres:latest"
    environment:
      - POSTGRES_DB=abc
      - POSTGRES_USER=abc
      - POSTGRES_PASSWORD=abc
    volumes:
      - pg_data:/var/lib/postgresql/data/
  django:
    build: .
    command: python /code/manage.py runserver 0.0.0.0:8004
    volumes:
      - .:/app
    ports:
      - 8004:8004
    depends_on:
      - postgres 

Unfortunately whenever I used to run docker-compose up then same err. used to pop up.

And this is how my database was defined in Django settings.py.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'abc',
        'USER': 'abc',
        'PASSWORD': 'abc',
        'HOST': '127.0.0.1',
        'PORT': '5432',
        'OPTIONS': {
            'client_encoding': 'UTF8',
        },
    }
} 

So, In the end I made use of docker-compose networking which means if I change the host of my database to postgres which is defined as a service in docker-compose.yml will do the wonders.

So, Replacing 'HOST': '127.0.0.1' => 'HOST': 'postgres' did wonders for me.

After replacement this is how your Database config in settings.py will look like.

DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'abc',
            'USER': 'abc',
            'PASSWORD': 'abc',
            'HOST': 'postgres',
            'PORT': '5432',
            'OPTIONS': {
                'client_encoding': 'UTF8',
            },
        }
    } 
Doom answered 9/12, 2020 at 22:49 Comment(0)
O
3

If you want to restart Postgresql on Linux, then you have to use the following command.

/etc/init.d/postgresql restart

Oilcan answered 30/10, 2017 at 0:53 Comment(0)
E
3

This is how I solved my problem:

see the status of services

brew services list

and the output was :

Name       Status  User Plist
postgresql error   myuser /Users/myuser/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
unbound    stopped 

I change the file name in this path, you can also remove it

mv /Users/myuser/Library/LaunchAgents/homebrew.mxcl.postgresql.plist /Users/myuser/Library/LaunchAgents/homebrew.mxcl.postgresql.plist_temp

and then reboot the os

sudo reboot

after booting I started the postgresql and it worked.

brew services start postgresql
Eustatius answered 17/6, 2021 at 12:29 Comment(1)
Thanks a ton! I have a deadline to submit my work and I got this error due to improper shutdown. You saved my day!!Dhobi
S
3
  1. Find postgresql@10 service directory
  $ ls /usr/local/var/postgresql@10

enter image description here

  1. Find file postmaster.pid and delete that file
  $ rm -f postmaster.pid
  1. Restart postgres service using
  $ brew services restart postgresql@10
Sadoff answered 5/7, 2021 at 5:30 Comment(0)
B
2

This worked for me: run

sudo lsof -i :<port_number>

after that it will display the PID which is currently attached to the process.

After that run sudo kill -9 <PID>

if that doesn't work, try the solution offered by user8376606 it would definitely work!

Board answered 25/10, 2017 at 12:32 Comment(0)
S
2

In my case

  1. I have changed the port in postgresql.conf file
  2. and restart postgresql services in

Run => service.msc => Restart

  1. now retry
Sorption answered 24/6, 2020 at 13:24 Comment(0)
D
1

First I tried

lsof -wni tcp:5432 but it doesn't show any PID number.

Second I tried

Postgres -D /usr/local/var/postgres and it showed that server is listening.

So I just restarted my mac to restore all ports back and it worked for me.

Daron answered 18/8, 2019 at 6:43 Comment(0)
S
1

For Docker users: In my case it was caused by excessive docker image size. You can remove unused data using prune command:

docker system prune --all --force --volumes

Warning: as per manual (docker system prune --help):

This will remove:

  • all stopped containers
  • all networks not used by at least one container
  • all dangling images
  • all dangling build cache
Stapler answered 27/8, 2019 at 19:38 Comment(0)
D
1

For me, the answer provided by @DivyanshuRawat's looks more like the real solution to this error.

As of this day; the 15th of March 2023 in my Django App, I experienced the same problem. And when I saw @DivyanshuRawat's answer, I tried it and it didn't work for me. I believe the reason it didn't work for me may have been the way he explained which sucessfully befuddled me, or my own impatience due to mental saturation. However, given what I know now, I wonder why it wasn't upvoted a lot given the fact that it made sense.

In my submission here, I will try to explain @DivyanshuRawat's answer a bit further, hopefully it can be clear to others facing the same problem and looking into answers here for leads to solving the error.

You need to note that;

  1. this error usually occurs when the networks section in your Docker Compose file is not formatted correctly.
  2. the error simply tells you that the service is unable to connect to the PostgreSQL database running in your docker database service, mostly because the hostname in your Django settings.py file is pointing to the web service container itself or something/somewhere else, rather than the docker database service container where the database is running.

Fixing this error;

  1. You have to make sure that networks section is defined at the top level of your Compose file, outside of any services, volumes, or other sections. Also ensure that it is formatted as a mapping, where each network is listed with a unique name, followed by its configuration options. This example below shows how the networks section should be formatted and this is the first thing I had to make sure I fix in my docker-compose.yml file:

    version: '3.9'
    
    services:
      web:
        # ...service configuration
    
        networks:
         - webnet
    
      db:
        # ....service configuration
    
        networks:
         - webnet
    
    networks:
      webnet:
        driver: bridge
    
  2. Changing the "HOST" parameter in your DATABASES setting in settings.py to the name of the db service container (which in this case is "db"), like this:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': os.environ.get('POSTGRES_DB'),
            'USER': os.environ.get('POSTGRES_USER'),
            'PASSWORD': os.environ.get('POSTGRES_PASSWORD'),
            'HOST': 'db', # change it to your docker-compose postgres service name. If you look at my own docker-compose.yml file, it is defined as 'db', so I changed this HOST config to 'db'
            'PORT': '5432',
        }
    }
    

After making the necessary changes, you can try rebuilding and running your docker containers with docker-compose up --build to see if it resolves the issue. At this point, everything worked for me.

Disabled answered 15/4, 2023 at 19:55 Comment(0)
S
0

I often encounter this problem on windows,the way I solved the problem is Service - Click PostgreSQL Database Server 8.3 - Click the second tab "log in" - choose the first line "the local system account".

Shyamal answered 16/10, 2017 at 2:50 Comment(0)
G
0

It also gives the same error if you just stop your PostgreSQL app. You just need to start it again. (PostgreSQL 11)

Gabbro answered 21/2, 2020 at 12:33 Comment(0)
E
0

I have faced the same issue and I was unable to start the postgresql server and was unable to access my db even after giving password, and I have been doing all the possible ways.

This solution worked for me,

For the Ubuntu users: Through command line, type the following commands:

1.service --status-all (which gives list of all services and their status. where "+" refers to running and "-" refers that the service is no longer running)

check for postgresql status, if its "-" then type the following command

2.systemctl start postgresql (starts the server again)

refresh the postgresql page in browser, and it works

For the Windows users:

Search for services, where we can see list of services and the right click on postgresql, click on start and server works perfectly fine.

Empower answered 16/6, 2020 at 12:33 Comment(0)
G
0

In my case I forgot to change the database from postgres (on my production) back to sqlite3 which I was using for development.

Galer answered 21/12, 2020 at 12:49 Comment(0)
U
0

It's working for me >>Node.Js App

user@MacBook-Pro % sudo lsof -i :5430

Output

COMMAND PID user FD TYPE DEVICE SIZE/OFF NODE NAME

node 7885 user 21u IPv6 0x2e7d89f6118f95b9 0t0 TCP *:radec-corp (LISTEN)

Kill the PID

user@MacBook-Pro % sudo kill -9 7885

One more test

user@MacBook-Pro % sudo lsof -i :5430

user@MacBook-Pro % "No more running PID for the port 5430"

Ultramicrochemistry answered 1/4, 2021 at 19:57 Comment(0)
E
0

In my case, on a Ruby on Rails project, I removed a .pid file from the folder tmp/pids and restart the system.

Eustatius answered 6/9, 2021 at 13:8 Comment(0)
G
0

Had the same issue. I checked that my database.yml file, (dev mode) host was pointing to 5433. I updated it to 5432 and it worked.

Gallenz answered 22/9, 2021 at 9:51 Comment(0)
F
0

Just in case someone needs this for windows, read on.

On windows hit the Windows button + R then enter services.msc and look for postgresql-x64-14, Right click it and click start

Then go back to your PgAdmin4 for windows and then enter your master password if asked. From here, you should be able to proceed as usual with viewing of the db schemas.

Also, for Django, restart your server with CTR+C then python manage.py runserver (assuming you're working inside a virtual env)

Good luck

Fruitarian answered 29/8, 2022 at 10:18 Comment(0)
M
0
ps -ef|grep postgres

Then kill the process with PID

sudo kill -9 PID

Then start the postgresql

sudo service postgresql start
Merwyn answered 27/12, 2022 at 5:49 Comment(0)
D
-1

In my case I when it happens to me I need to do the following steps:

1º Step

Log in postgres user: #sudo su postgres

2º Step

Run the following steps: /opt/PostgreSQL/10/bin/postgres -D /opt/PostgreSQL/10/data -r /usr/local/var/postgres/server.log

Explanation:

We access the utility of postgres located at /opt/PostgreSQL/10/bin/ in your case could be another but identify where it's.

After this step we tell to the utility of postgres where the it's data folder is by using the option -D, this data folder contains all necessary configuration of postgres server.

The option -r we tell to postgres where to send stdout and stderr to given file, in my case the file that I used is /usr/local/var/postgres/server.log

Note:

  • I'm using Postgre 10
  • Linux Ubuntu
Degrease answered 22/2, 2023 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.