Thread: could not bind IPv4 address "127.0.0.1": Address already in use

could not bind IPv4 address "127.0.0.1": Address already in use

From
Siddharth Jain
Date:
Hi All,

I am trying to run Postgres 14 on a Mac OS. I installed Postgres from EDB and ran initdb and it gave me this:

❯ initdb
The files belonging to this database system will be owned by user "xxx".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/pgsql/14/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... America/Los_Angeles
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /var/lib/pgsql/14/data -l logfile start

But when I try to start the server I get this:

❯ pg_ctl -D /var/lib/pgsql/14/data start
waiting for server to start....2023-03-07 17:16:43.226 PST [25925] LOG:  starting PostgreSQL 14.7 on x86_64-apple-darwin20.6.0, compiled by Apple clang version 12.0.0 (clang-1200.0.32.29), 64-bit
2023-03-07 17:16:43.228 PST [25925] LOG:  could not bind IPv6 address "::1": Address already in use
2023-03-07 17:16:43.228 PST [25925] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2023-03-07 17:16:43.228 PST [25925] LOG:  could not bind IPv4 address "127.0.0.1": Address already in use
2023-03-07 17:16:43.228 PST [25925] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2023-03-07 17:16:43.228 PST [25925] WARNING:  could not create listen socket for "localhost"
2023-03-07 17:16:43.228 PST [25925] FATAL:  could not create any TCP/IP sockets
2023-03-07 17:16:43.229 PST [25925] LOG:  database system is shut down
 stopped waiting
pg_ctl: could not start server
Examine the log output.


I ran:

 ~/ netstat -ln -p tcp | grep 5432

and there is no output. I also tried:

❯ pg_ctl status
pg_ctl: no server running

What is wrong here and how to fix?

S.

Re: could not bind IPv4 address "127.0.0.1": Address already in use

From
Tom Lane
Date:
Siddharth Jain <siddhsql@gmail.com> writes:
> But when I try to start the server I get this:

> 2023-03-07 17:16:43.228 PST [25925] LOG:  could not bind IPv6 address
> "::1": Address already in use
> 2023-03-07 17:16:43.228 PST [25925] HINT:  Is another postmaster already
> running on port 5432? If not, wait a few seconds and retry.
> 2023-03-07 17:16:43.228 PST [25925] LOG:  could not bind IPv4 address
> "127.0.0.1": Address already in use
> 2023-03-07 17:16:43.228 PST [25925] HINT:  Is another postmaster already
> running on port 5432? If not, wait a few seconds and retry.

That's pretty clear I think: the ports are already bound.

> I ran:
>   netstat -ln -p tcp | grep 5432
> and there is no output.

This is the wrong incantation for this purpose.  Try

netstat -an -p tcp | grep 5432

I expect you'll see a couple of ports in LISTEN state.  Unfortunately,
netstat is not much help at locating the processes listening to such
ports.  This might be more help:

lsof -n | grep 5432

            regards, tom lane



Re: could not bind IPv4 address "127.0.0.1": Address already in use

From
Siddharth Jain
Date:
Thanks Tom. found the problem.

On Tue, Mar 7, 2023 at 8:28 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
Siddharth Jain <siddhsql@gmail.com> writes:
> But when I try to start the server I get this:

> 2023-03-07 17:16:43.228 PST [25925] LOG:  could not bind IPv6 address
> "::1": Address already in use
> 2023-03-07 17:16:43.228 PST [25925] HINT:  Is another postmaster already
> running on port 5432? If not, wait a few seconds and retry.
> 2023-03-07 17:16:43.228 PST [25925] LOG:  could not bind IPv4 address
> "127.0.0.1": Address already in use
> 2023-03-07 17:16:43.228 PST [25925] HINT:  Is another postmaster already
> running on port 5432? If not, wait a few seconds and retry.

That's pretty clear I think: the ports are already bound.

> I ran:
>   netstat -ln -p tcp | grep 5432
> and there is no output.

This is the wrong incantation for this purpose.  Try

netstat -an -p tcp | grep 5432

I expect you'll see a couple of ports in LISTEN state.  Unfortunately,
netstat is not much help at locating the processes listening to such
ports.  This might be more help:

lsof -n | grep 5432

                        regards, tom lane