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.
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).
C:\Users\I501807>docker run -d postgres
cae212eb5ac3dbb4aa374eb32e380d2cbc374e1fab577e7a368fa4d5ed5e8cd6
C:\Users\I501807>docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
C:\Users\I501807>docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
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.
update:
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”?
root@7624ea1a5b62:/#
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
root@cff45e74daba:/#
vs.
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!
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??)