0

How do I do things like pg_dump and psql outside of the dockerized postgres server?

  1. Pull and run the latest postgres image
docker run --name postgresqldb -e POSTGRES_PASSWORD=postgres -e POSTGRES_USER=postgres -e  POSTGRES_HOST_AUTH_METHOD=password -p 5432:5432 -d postgres
  1. Attempt to use psql
➜ git:(development) ✗ psql
zsh: command not found: psql

What I'm trying to do is start up a postgres server container and utilize that container to run database exports (pg_dump) directly out of my terminal, not through use of the container with docker exec Though my terminal doesn't know what psql is. Is there a way I can hit this container through the port mappings? How would I do that?

Thomas
  • 2,622
  • 1
  • 9
  • 16
  • 1
    You would need to install psql on your computer to be able to use the command. psql is a command line client for postgres databases. See [this question](https://stackoverflow.com/questions/44654216/correct-way-to-install-psql-without-full-postgres-on-macos) on how to install on mac without installing the whole server alongside it (which I assuming you are trying to avoid by dockerizing the server). – JNevill Sep 21 '22 at 18:49
  • @JNevill I'll check this out, I was thinking that I could access the services on the container through the port. Is this incorrect? – Thomas Sep 21 '22 at 18:50
  • 1
    Yes. That's exactly what you want to do. You have to have a program/application to communicate in postgres-speak through that port to your postgres server running in your container. That program, in this case, is `psql`. [Check out this very similar question that has a lot of great info in the answers](https://stackoverflow.com/questions/37694987/connecting-to-postgresql-in-a-docker-container-from-outside). – JNevill Sep 21 '22 at 18:52
  • @JNevill Thanks, I had previously checked that out but the psql command wasn't working. Putting these two answers together now allow me to use the dockerized container outside of the container. The important information here is that `psql` is a tool to be used with the postgres server, not the server itself. Seems only reasonable that this would be the case but for some reason I had that assumption. – Thomas Sep 21 '22 at 19:09
  • 1
    It's a common misconception. Folks starting out with databases for the first time routinely confuse their client application with the server itself or don't understand that a server needs a client in order to interact with the server. Countless questions pop up on here with people thinking `myphpadmin` or `SSMS` is their actual server, for instance. Once you get your head wrapped around it, then databases start making a lot more sense. – JNevill Sep 21 '22 at 19:24
  • Any particular reason you don't want to run it inside the container? I much prefer that because then I don't need to install anything on the host and I'm always sure that the psql version matches the database. – Hans Kilian Sep 21 '22 at 19:55
  • I have multiple containers. I have some code from one container that wants to use postgres tools like psql and pg_dump but not host a server. I can't access the contents of the postgres server container because its contained. – Thomas Sep 22 '22 at 15:15

0 Answers0