DockerfilePostgresDatabasebackendsoftware developmentrails 7 apiAPI

5 min read

|

August 13, 2024

How to Manage A Rails 7 API Postgres Database in Docker

Learn to manage your Rails 7 Postgres database within Docker through our clear, step-by-step tutorial.



Chapters

Introduction

The Rails way

The Postgres container

Database dump

Next up

Introduction

In this blog, I will show you different approaches to managing your Rails 7 API Postgres database in Docker environment. This blog is an extension of How to containerize an existing Rails 7 API with Docker. If you are new to containerizing Rails 7 applications, you should first visit this blog. Once you are familiar with Dockerizing Rails 7, come back here for instructions on how to manage a Rails 7 Postgres database in a Docker environment. Before doing anything, make sure that you have two containers running (Rails application and postgres container). You can check running containers with: $ docker ps . You should get something like this: 7e9db575c1f9 app_name-app   "bundle exec rails s…"   2 seconds ago   Up 1 second    0.0.0.0:3000->3000/tcp app_name-app-1c088295b12d3   postgres:16.2-bullseye "docker-entrypoint.s…"  2 days ago Up 32 seconds   0.0.0.0:5432->5432/tcp app_name-db-1

The Rails way

When working with a Rails application inside a Docker environment, the process for database-related tasks and accessing the Rails console remains largely the same as in a standard Rails setup. However, there's a slight difference in the command syntax due to Docker.Instead of running commands like rails db:create directly, you'll need to prefix them with  $ docker-compose exec app followed by bin/rails. Here's how you can perform common Rails tasks within Docker:

  • Creating the Database: $ docker-compose exec app bin/rails db:create
  • Running Migrations: $ docker-compose exec app bin/rails db:migrate
  • Seeding the Database: $ docker-compose exec app bin/rails db:seed
  • Accessing the Rails Console: $ docker-compose exec app bin/rails console

By prefixing your Rails commands with $ docker-compose exec app bin/rails, you ensure that the commands are executed within the Docker environment of your Rails application.

The Postgres container

 We can connect to the database with psql inside a Postgres container. You can connect to a Postgres container with: $  docker compose exec db bash. You can find it with $ docker ps. Once you are connected to the container, you can connect to the database with SQL, e.g., $ psql -U username database_name, and do whatever you want (ALTER TABLES, USERS, SCHEMAS).

Database dump

If you have an existing database that you want to import to the Postgres database in Docker. Here is how to do it. I assume that you have a dump file in .sql format; if you don’t, you can always make it with: pg_dump name_of_database > path/where/you/want/to/save/name_of_the_file.sql 

The next step is to copy the file you just created to a Postgres Docker container.docker cp <path_to_file> <containter_id>:<destination_path> . Once again, connect to the Postgres container with $ docker compose exec db bash, and finally, you can restore with: psql database_name < name_of_the_file.sql .  That's it!

Alternative: Loading Database Dump via psql Client on the Host Machine

Instead of copying the .sql dump file to the Docker container, you can use the psql client installed on your host machine to connect directly to the Postgres instance running inside the Docker container. Here’s how you can do that:

Step 1: Install the psql Client

If you don’t already have the psql client installed on your host machine, you can install it with the following command:

 $ sudo apt-get install postgresql-client

Once the psql client is installed, you can connect directly to the Postgres database running in your Docker container.

Step 2: Connect to the Postgres Database in Docker

To connect to the Postgres instance running inside the Docker container, you need the following information:

  • Host: Use localhost or the IP address where your Docker container is running.
  • Port: Default Postgres port is 5432.
  • Database Name: The name of the database you want to restore to.
  • Username: The username for the Postgres database.
  • Password: The password for the Postgres user.

Run the following command to connect from your host machine to the Postgres database running in Docker:

 $ psql -h localhost -p 5432 -U postgres -d database_name

If Docker is running on a different host or you've exposed Postgres to another port, update the host and port accordingly.

Step 3: Restore the Database Dump

Once you’ve confirmed the connection to the Postgres instance, you can restore the database dump directly from your host machine:

$ psql -h localhost -p 5432 -U postgres -d database_name -f /path/to/your/dump.sql

  • Replace /path/to/your/dump.sql with the actual path to your .sql dump file on the host machine.
  • You will be prompted for the Postgres user’s password unless you've set up passwordless access.

This method saves you from having to copy the dump file into the Docker container, making the process quicker, especially for larger dump files.

Now you have two ways to restore a Postgres database—either by copying the dump into the Docker container or by using the psql client installed on your host machine. Both methods are effective, so choose the one that best fits your workflow!

Next up

Explore what else Docker can help you optimize:

Share


WRITTEN BY

author

Software Engineer

Dario truly cares about the projects he works on and it shows in the quality of his work. He's always picking up new skills and loves to share what he learns with you.

UP NEXT

How to Deploy & Run a Rails 7 API on a Remote Server with Docker

Deploy your Rails 7 API on a remote server using Docker. Learn key steps, configurations, and tips for smooth operation.