[RUBY] [Docker] How to forcibly disconnect the session connected to the database [Postgres]

When I tried Rails db: migrate: reset, I got an error I had never seen and got stuck a bit, so I'd like to share the solution here.

ERROR!


ERROR:  database "hoge_development" is being accessed by other users
DETAIL:  There are 1 other sessions using the database.

To be honest, I wasn't sure why this happened, but it seems that other users kept connecting and couldn't access the db container.

You need to go inside the container, connect to PostgreSQL and force delete the connection.

【environment】

Solution procedure

First, enter the db container.


$ docker exec -it hoge_db_1 bash
root@xxxxxaf78adb:/#

Once inside the container, connect to PostgreSQL.


root@xxxxxaf78adb:/# psql -U postgres

After connecting to PostgreSQL, display the pid of the connecting user with the following command.


postgres=# SELECT pid FROM pg_stat_activity where pid <> pg_backend_pid();

  pid  
-------
144
29
28
27
(4 rows)

Since four came out in ↑, execute the following commands one by one in order to disconnect all.


SELECT pg_terminate_backend(144);
 pg_terminate_backend 
----------------------
 t
(1 row)

SELECT pg_terminate_backend(29);
 pg_terminate_backend 
----------------------
 t
(1 row)

(abridgement)

I did this, got out of the container and `` `docker-compose exec web rails db: migrate: reset``` and it passed!

However, it is unclear why other users are connected even after checking, so if you can understand it, I would appreciate it if you could comment! !!

Thank you for reading to the end!

Almost every day, I output what I got stuck in my personal development. If you have any suggestions, I would appreciate it if you could comment!

Recommended Posts

[Docker] How to forcibly disconnect the session connected to the database [Postgres]
How to check the logs in the Docker container
How to delete the database when recreating the application
How to install Docker
How to set Docker nginx
How to build Rails, Postgres, ElasticSearch development environment with Docker
[Rails] How to display information stored in the database in view
[Docker] How to access the host from inside the container. http://host.docker.internal:
How to check the database of apps deployed on Heroku
I examined the concept of the process to understand how Docker works
How to use the link_to method
How to use the include? method
Dynamically switch the database to connect to
How to use the form_with method
How to find the average angle
How to use the wrapper class
How to run JavaFX on Docker
[Artifactory] How to use Docker repository
How to add the delete function
How to build CloudStack using Docker
How to start Camunda with Docker
Gorigori beginners summarized how to operate the database using rails console Ntiunus
[Docker] How to solve the error function not implemented @ io_fread ~ [Super easy]
[Docker] How to build when the source code is bind-mounted on the container
About the trap that the database (DB) becomes unusable due to Docker update
How to get the setting value (property value) from the database in Spring Framework
[Java] How to use the File class
Investigate the replacement from Docker to Podman.
How to delete the wrong migration file
[Java] How to use the hasNext function
How to put out the error bundling
[Rails] How to use rails console with docker
[Java] How to use the HashMap class
[Rails] How to use the map method
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to determine the number of parallels
[Java] How to set the Date time to 00:00:00
[Swift] How to implement the countdown function
How to change the timezone on Ubuntu
Ransack sort_link How to change the color!
[Processing × Java] How to use the class
How to sort the List of SelectItem
How to find the tens and ones
How to pass the value to another screen
How to get the date in java
[Processing × Java] How to use the function
[Java] How to use the Calendar class
Summarized how to climb the programming stairs
How to run Blazor (C #) with Docker
How to build Rails 6 environment with Docker
How to display the amount of disk used by Docker container for each container