Search code examples
postgresqlpsql

Unable to build the PostgreSQL environment. FATAL: database "t" does not exist


I installed postgres from brew. I got an error, I tried to delete the postmaster.id The file seems to be missing.

rm -f data/postmaster.pid It had no effect.

I tryed

$brew install postgres
$psql --version                                                                                                        
psql (PostgreSQL) 14.1

$postgres --version                                                                                                    
postgres (PostgreSQL) 14.1
$psql

psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  database "t" does not exist
$postgres -D /usr/local/var/postgres                                                                                  

2022-01-28 14:27:14.406 JST [94892] LOG:  starting PostgreSQL 14.1 on aarch64-apple-darwin21.1.0, compiled by Apple clang version 13.0.0 (clang-1300.0.29.3), 64-bit
2022-01-28 14:27:14.408 JST [94892] LOG:  could not bind IPv6 address "::1": Address already in use
2022-01-28 14:27:14.408 JST [94892] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2022-01-28 14:27:14.408 JST [94892] LOG:  could not bind IPv4 address "127.0.0.1": Address already in use
2022-01-28 14:27:14.408 JST [94892] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2022-01-28 14:27:14.408 JST [94892] WARNING:  could not create listen socket for "localhost"
2022-01-28 14:27:14.408 JST [94892] FATAL:  could not create any TCP/IP sockets
2022-01-28 14:27:14.409 JST [94892] LOG:  database system is shut down
$rm /usr/local/var/postgres/postmaster.pid                                                                            

rm: /usr/local/var/postgres/postmaster.pid: No such file or directory    
$sudo lsof -i:5432                 
                                                                                    
COMMAND    PID USER   FD   TYPE            DEVICE SIZE/OFF NODE NAME
postgres 94724    t    7u  IPv6 0xe42d991dab624b7      0t0  TCP localhost:postgresql (LISTEN)
postgres 94724    t    8u  IPv4 0xe42d991d7856c0f      0t0  TCP localhost:postgresql (LISTEN)

Solution

  • I don't know why you insist in wrecking your PostgreSQL server by removing the PID file. I wish you would stop doing that.

    The error message means that you are trying to connect to database t, but that database does not exist. Since you do not explicitly specify a database, the causes could be:

    • you are operating system user t, so the connection defaults to database t

    • the environment variable PGDATABASE is set to t

    • the environment variable PGUSER is set to t, and the connection defaults to the same database name as the user name

    In any event, the solution is to pick an existing database and use

    psql -d dbname
    

    To find out which databases actually exist on the server, you can use

    psql -l