Unable to run psql inside a postgres container


I am trying to run psql command inside a postgres docker container using below command:
docker run --rm -it postgres bash
But I get this error:

docker run --rm -it postgres bash
root@3615146cf679:/# psql
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?

I am not sure what’s wrong? I was able to run this command sometime back.
Any help would be appreciated.

Kush Sharma

Normaly people run the database in the the container and docker exec into the running container to access the local database. From what I see you only try to use the psql command, but want to connect to a remote datbase instance, aren’t you?

Hi Metin,
The problem with normal approach is that if I execute:
docker run --rm -d postgres
Then “docker ps” command has no running containers which means that container was indeed started but then stopped. I could verify it by docker ps -a

Basically, I want to run a postgres container and run psql queries inside the container like create table etc etc.

I am not sure what to make of this. The --rm option deletes the container immediatly, so docker ps -a can not list a container that is immediatly deleted when exitet.

Please share the exact commands and the errors from .the container logs (hint: remove --rm from your command).

I removed --rm and just used:

docker run -d postgres

Here is the complete output:

C:\Users\I501807>docker run -d postgres

C:\Users\I501807>docker ps

C:\Users\I501807>docker ps -a
cae212eb5ac3 postgres “docker-entrypoint.s…” About a minute ago Exited (1) About a minute ago keen_pike

So, even if I remove --rm, then as well, the container is not in running state and as a result, I cannot execute docker exec <container_id> bash command to run psql queries on stopped container.

Of course you can not exec in a stopped container.
But you can do docker logs cae212eb5ac3 to get the logs and see what cause the container to exit.
This is the other part of what I was asking for.

My guess: you missed a mandatory environment variable described on the Dockerhub page.

the instruction to “start a postgres instance” on the Dockerhub description is:

$ docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

further below in the description you find:

This environment variable is normally required for you to use the PostgreSQL image. This environment variable sets the superuser password for PostgreSQL. The default superuser is defined by the POSTGRES_USER environment variable.

Hi Metin,
Thanks, with “POSTGRES_HOST_AUTH_METHOD=trust” variable, I can keep a container in running state.

But, I am still not able to use psql commands. I am using following command now to run a container and interactively open a bash inside this running container as well and it gives an error on using psql commands:

C:\Users\I501807>docker run -e POSTGRES_HOST_AUTH_METHOD=trust -e POSTGRES_USER=psotgres -e POSTGRES_PASSWORD=postgres -it postgres bash
root@7624ea1a5b62:/# psql -U postgres
psql: error: could not connect to server: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket “/var/run/postgresql/.s.PGSQL.5432”?

I would appreciate more help.

You are causing the problems yourself. Can you please try the exact command I provided early, without replacing any parts? You do make the entrypoint script skip database start because you add “bash” as an argument to the container. Don’t do that.

me@swarm1:~$ docker run -e POSTGRES_PASSWORD=postgres -it postgres bash


me@swarm1:~$ docker run -e POSTGRES_PASSWORD=postgres -it postgres
The files belonging to this database system will be owned by user “postgres”.
This user must also own the server process.

The database cluster will be initialized with locale “en_US.utf8”.
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/postgresql/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 … Etc/UTC
creating configuration files … ok
running bootstrap script … ok
performing post-bootstrap initialization … ok
syncing data to disk … ok

You should always first try exactly what the Dockerhub descriptions say, before trying to modify somethine on the run command…

btw. I removed -d for faster troubleshooting, you should add it in your final command again.

Hi Metin,
Right, I got it. I was trying to modify the original command in order to do everything in just one line. My bad.
Finally I noticed the difference when I dont use “bash”. Thanks! Now, I am able to execute psql queries inside my container. Sorry for this trouble, and thanks for your patience with me! :slight_smile:

Just one more question as you said in your first reply, Is it possible to connect to a service instance of postgresql available on Cloud using docker on my local machine? (I mean with no tunneling required??)

Somehow I have the feeling that you didn’t read the Dockerhub description at all, did you?

The two entries underneath “How to use this image” provide the answer for your initial question and your follow up question…

To connect to another postgres instance, the desciption says:

docker run -it --rm --network some-network postgres psql -h some-postgres -U postgres

I assume you know how to use psql. If you don’t then run docker run -it --rm postgres psql --help to check the parameters.