140

Problem

I'm trying to start postgres in a docker container on my Mac, but I keep getting the following error message

docker: Error response from daemon: driver failed programming external connectivity on endpoint postgres (8392b9e5cfaa28f480fe1009dee461f97e82499726f4afc4e916358dd2d2f61e): Error starting userland proxy: Failed to bind tcp 0.0.0.0:5432 address already in use.

I have postgres installed locally, but I stopped it and running

pg_ctl status

returns

pg_ctl: no server running

I've ran the following to check what's running on 5432

lsof -i tcp:5432

&

netstat -anp tcp | grep 5432

and nothing is running on the port.

Versions

Mac - OS X El Capitan Version 10.11.2

PostgreSQL - 9.5

Docker - Docker version 1.12.0-rc2, build 906eacd, experimental

0

15 Answers 15

168

If lsof -i :5432 doesn't show you any output, you can use sudo ss -lptn 'sport = :5432' to see what process is bound to the port.

Proceed further with kill <pid>

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

7 Comments

With this command I get "sudo: ss: command not found" - any idea what is causing that?
It appears that system-wide Postgres service is running on that port. First we kill it and then allow docker-compose occupy that port.
Use the following command to get the processes: sudo lsof -i :5432
Running sudo lsof -i :5432 then kill <pid> worked for me.
you need to use kill -9 pid for macos
|
63

If you execute lsof -i :5432 on the host you can see what process is bound to the port.

Some instance of Postgres is running. You can execute kill <pid> to kill it if you want. You can also use 5432 instead of 5432:5432 in your docker command or docker-compose file and let docker choose the host port automatically.

3 Comments

@blockloop yes i see one postgres process with pid 144 running.
run sudo lsof -i :5432, and then run kill <PID>
It's necessary to run the command as super user.
55

The first thing you should do is stop PostgreSQL service. In most cases it fixed the issue.

sudo service postgresql stop

If above doesn't work. then add the following line to /etc/postgresql/12/main/postgresql.conf

sudo vim /etc/postgresql/12/main/postgresql.conf

## good if you add under CONNECTION AND AUTHENTICATION comments
listen_addresses = "*"

Comments

42

macOS Monterey

None of the above commands worked for me - need to do few changes. So, adding the complete working solution:

  1. Identify what is running in port 5432: sudo lsof -i :5432

  2. Kill all the processes that are running under this port: sudo kill -9 <pid>

  3. Run the command again to verify no process is running now: sudo lsof -i :5432

enter image description here

Comments

12

it is worked for me, probably you should stop postgres :

sudo systemctl stop postgresql

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
5

For MacOS:

sudo lsof -i:5432
  1. This should output a list of processes listening on port 5432 - which is the default Postgresql server port. If you chose a different default port during installation, you of course should use that port number instead of 5432.

sudo kill -15 <process id>

  1. With that list, you can run this command to end the Postgresql server process. The process id can be found in the PID column of the output.

1 Comment

I think this does not add anything new to the answers.
4

In some cases it is critical to perform a more in-depth debugging to the problem before stopping or killing the container/process.

Consider following the checklist below:

1) Check you current docker compose environment
Run docker-compose ps.
If port is in use by another container, stop it with docker-compose stop <service-name-in-compose-file> or remove it by replacing stop with rm.

2) Check the containers running outside your current workspace
Run docker ps to see list of all containers running under your host.
If you find the port is in use by another container, you can stop it with docker stop <container-id>.
(*) Because you're not under the scope of the origin compose environment - it is a good practice first to use docker inspect to gather more information about the container that you're about to stop.

3) Check if port is used by other processes running on the host
For example if the port is 6379 run:

$ sudo netstat -ltnp | grep ':6379'
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      915/redis-server 12 
tcp6       0      0 ::1:6379                :::*                    LISTEN      915/redis-server 12

(*) You can also use the lsof command which is mainly used to retrieve information about files that are opened by various processes (I suggest running netstat before that).

So, In case of the output above the PID is 915. Now you can run:

$ ps j 915
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
    1   915   915   915 ?           -1 Ssl    123   0:11 /usr/bin/redis-server 127.0.0.1:6379

And see the ID of the parent process (PPID) and the execution command.
You can also run: $ pstree -s <PID> to a visual display of the process and its related processes (install with: brew install pstree).

In our case we can see that the process probably is a daemon (PPID is 1) - In that case consider running:
A) $ cat /proc/<PID>/status in order to get a more in-depth information about the process like the number of threads spawned by the process, its capabilities, etc'.
B) $ systemctl status <PID> in order to see the unit that caused the creation of a specific process. If the service is not critical - you can stop and disable the service.

4) Restart Docker service
Run sudo service docker restart.

5) You reached this point and..
Only if its not placing your system at risk - consider restarting the server.

Comments

3

None of these other answers worked for me. (For example, lsof and netstat just returned empty lines.) The following worked, though:

sudo -u postgres pg_ctl -D /Library/PostgreSQL/13/data stop

2 Comments

using mac os iterm i got this response: sudo: pg_ctl: command not found
Might have to change 13 to your version of postgres, but this worked for me on mac
3
  1. First you have to compose down:

    docker compose down

  2. Then, check if still listens:

sudo lsof -i :5432

  1. If you spot any user, you can kill with:

    lsof -t -i tcp:5432 | xargs kill

  2. Also you can get some helper, If all of these codes confused you. Install freeport:

    pip install freeport

  3. Then use it: freeport 5432

This will close every port listener and established user.

  1. Also a big reminder: Some of the Mac apps use ports, for example port 5000 is used in many MACOS for AirPlay Receiver, you can disable it from Settings>AirPlay Receiver> Turn Off if you are using a MacBook.

  2. In the worst case scenario, shift the port written in your project.

Comments

2

In case of mac,

  • if you are OK with uninstalling the POSTGRES for the time being:
brew uninstall postgres

Then check if the process still exists

sudo lsof -nP -i4TCP:5432 | grep LISTEN

If it exists, then kill it

kill -9 <pid>

Check again if the 5432 is being listened at, this time it should not be.

Comments

2

This command line is very simple and easy to remember, using third party javascript packages. npx comes built in with Node.js:

npx kill-port 3000

For a more powerful tool with search:

npx fkill-cli

Comments

1

Go to project and click on docker-compose.yml

version: '2'

services: web: build: . ports: - "8000:8000" volumes: - .:/app links: - db - mail-server db: image: "postgres" environment: POSTGRES_PASSWORD: hunter2 ports: - "5432:9432" mail-server: image: "mailhog/mailhog" expose: - 1025 ports: - "8026:8026"

" change the ports to 8026:8026 because there is already running another container on this port number only change the port number"

1 Comment

change port No in your docker-compose file because you are running another one .run this command " docker-compose up --build"
1

I tried to sudo kill -9 <PID> to disable postgres process, but it spawns again and again with a different PID. After that, I found that it stores a process under LaunchDemos and it runs on every startup:

  • cd /Library/LaunchDemos/
  • sudo rm com.edb.launchd.postgresql-13.plist
  • Restart your PC to apply changes.

Comments

1

In case you have installed Postgres locally and set it to use port 5432 (or any other), you can try changing the set port number. in your docker-compose file to a different available port number; e.g.

FROM

image: postgres:16
    ports:
      - "5432:5432"

TO

image: postgres:16
    ports:
      - "5433:5433"

Confirm port availability before changing.

Comments

0

lsof -i :5432 gave no output for me. going to MacOS's Activity Monitor, searching for postgres, selecting all of the results, then clicking on the ⓧ to kill them fixed it for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.