skip to Main Content

I am attempting to create database in postgres docker container using createdb command.

It creates database, but I also get an error message:
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory

and I am not sure what is happening, why am I getting this error if I am not attempting to connect to the database, and how is database successfully created if error occurred ?

I am running this command from a terminal and I am not sure how does it know that it is suppose to create database in docker container and not in local postgresql server so I am curious how does it work. I suppose it has something to do with postgresql.config file but my postgres docker container is listening on local port 5433 (not default one).

If there is anyone who is willing to explain me all this, I would be grateful. Cheers !

2

Answers


  1. Chosen as BEST ANSWER

    After a little bit of digging I realised my mistake.

    I was running createdb command through Makefile target and that target before createdb command checked if database exists, but I wrote that part wrongly.

    Instead of writting it as:

    @if PGPASSWORD=$(DB_PASSWORD) psql -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) -lqt | cut -d | -f 1 | grep -qw $(DB_NAME); then <rest> 
    

    I wrote :

    @if psql -lqt | cut -d | -f 1 | grep -qw $(DB_NAME); then <rest>
    

    so that caused a problem.

    This is the whole Makefile target that now works properly if anyone is interested:

    create-database:
        @if PGPASSWORD=$(DB_PASSWORD) psql -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) -lqt | cut -d | -f 1 | grep -qw $(DB_NAME); then 
            echo "Database already exists"; 
        else 
            PGPASSWORD=$(DB_PASSWORD) createdb -h $(DB_HOST) -p $(DB_PORT) -U $(DB_USER) $(DB_NAME); 
            echo "Database created"; 
        fi
    

    Thank you @Lie Ryan for your answer, it helped me find my mistakes. Cheers !


  2. If you aren’t using the default port, you need to tell createdb, psql, and pretty much all the other Postgres CLI where to find the database server.

    These tools basically just connects to the database and then runs SQL commands, they don’t magically find your postgres instance.

    When connecting to a localhost instance, psql might try to connect using local socket files instead of via TCP, but this only works if your local database is actually running natively locally (i.e. not in a container/VM).

    If you’re starting a database in a container using the official postgres docker image, the container initialisation script usually would already create and start the database for you when the container starts, so you don’t usually need to call createdb yourself. You can influence how the container/database initialisation by providing a couple -e (environment variable) flags to provide the parameters at first container startup. Refer to the postgres image’s documentation for these environment variables.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search