Docker Makes Setting Up PostgreSQL Super Easy!

Hello, friends!

I recently was working on a blog application in Adonis.js 6, and I ran into the issue of setting up a database. Originally, I set up the project with SQLite. It’s a very easy database solution, because the database is just a file in your project folder, with no database server or credentials required. However, while it can be used in production, I have seen that every tech job I’ve ever worked at used a heavier database, such as PostgreSQL, so I figured I would switch to that, just for the sake of good practice.

I began to sweat a bit at the idea of having to install Postgres, then run it, then have to Google some weird issue, since sometimes some config file internal to Postgres has to be modified, depending on which operating system I’m on. So on and so forth.

But then I remembered that Docker exists, and in fact, I have used Docker on the job to make quick work of setting up and connecting to existing databases! Especially the Postgres kind! 😀

With this idea fresh in mind, I set out to search for how to use Docker to create a new Postgres database. The idea here is that the database will run inside a little container, which is basically sort of a mini-computer that runs on your system, isolated from the rest of the OS. Assuming you have Docker installed and running, you can connect to this database and use it for development purposes using these commands:

docker pull postgres

docker run --name devdb -e POSTGRES_PASSWORD=mysecretpassword -p 5432:5432 -d postgres

To save you a bit of headache, I’ve explicitly specified port 5432, even though that’s the port PostgreSQL uses by default, as I was having port issues when trying to set up the database locally. Thanks for the tip, Claude! 😀

Once you run these commands, you should see something like this in your Docker Desktop window:

This means that your database is running, and you can connect to it and view its contents by supplying a database viewer like pgAdmin with the credentials in the command. Here’s what that looks like:

Since the database is running locally, we just use localhost as the hostname. As per the Docker command I showed you earlier, the password is mysecretpassword. Don’t tell anyone!

Common Pitfalls

If you run into issues connecting to the database, make sure you entered the username, password, hostname, and port (5432) correctly. Also, make sure that Docker Desktop is running, and that the container some-postgres is running inside Docker, as well. Also, make it so that PostgreSQL is running only in Docker, and not somewhere else in your OS, as this can cause weird conflicts.

I hope this article has helped you get set up with PostgreSQL and Docker! If you found this article helpful or interesting, please share it with your friends, and subscribe so you can see what I write in the future!

Sources:

https://www.docker.com/blog/how-to-use-the-postgres-docker-official-image

https://stackoverflow.com/questions/5598517/find-the-host-name-and-port-using-psql-commands


Discover more from Shaffan's Blog

Subscribe to get the latest posts sent to your email.

Comments

Leave a Reply

Discover more from Shaffan's Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading