Help us understand the problem. What is going on with this article?

Docker上のLaravelでDBをmigrateしたらエラー(could not connect to server: Connection refused)になった話

More than 1 year has passed since last update.

はじめに

laradockにて構築した、laravel+postgreSQLの環境でmigrateを行ったところ、エラーになりました。

1. マイグレーションファイルの作成

laradock $ docker-compose exec workspace bash
root@16a77d3a94fe:/var/www# cd quickstart/
root@16a77d3a94fe:/var/www/quickstart# php artisan make:migration create_tasks_table --create=tasks
Created Migration: 2019_04_10_151437_create_tasks_table

2. migrate

root@16a77d3a94fe:/var/www/quickstart# php artisan migrate

3. エラー

In Connection.php line 664:

  SQLSTATE[08006] [7] could not connect to server: Connection refused                                                    
        Is the server running on host "localhost" (127.0.0.1) and accepting                                                   
        TCP/IP connections on port 5432?                                                                                      
  could not connect to server: Cannot assign requested address                                                           
        Is the server running on host "localhost" (::1) and accepting                                                         
        TCP/IP connections on port 5432? (SQL: select * from information_schema.tables where table_schema = public and table  
  _name = migrations)                                                                                                    


In Connector.php line 67:

  SQLSTATE[08006] [7] could not connect to server: Connection refused   
        Is the server running on host "localhost" (127.0.0.1) and accepting  
        TCP/IP connections on port 5432?                                     
  could not connect to server: Cannot assign requested address          
        Is the server running on host "localhost" (::1) and accepting        
        TCP/IP connections on port 5432?                                     

対処法

laravelアプリケーションのディレクトリ配下にある、.envファイルを修正します。

DB_HOST=localhostであったところを、laradock/docker-compose.yml中でPostgreSQLに指定されたサービス名postgresを用いて、DB_HOST=postgresに変更します。

docker-compose.yml
### PostgreSQL ###########################################
    postgres:
      build: ./postgres
      volumes:
        - ${DATA_PATH_HOST}/postgres:/var/lib/postgresql/data
        - ${POSTGRES_ENTRYPOINT_INITDB}:/docker-entrypoint-initdb.d
      ports:
        - "${POSTGRES_PORT}:5432"
      environment:
        - POSTGRES_DB=${POSTGRES_DB}
        - POSTGRES_USER=${POSTGRES_USER}
        - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
        - GITLAB_POSTGRES_INIT=${GITLAB_POSTGRES_INIT}
        - GITLAB_POSTGRES_USER=${GITLAB_POSTGRES_USER}
        - GITLAB_POSTGRES_PASSWORD=${GITLAB_POSTGRES_PASSWORD}
        - GITLAB_POSTGRES_DB=${GITLAB_POSTGRES_DB}
        - JUPYTERHUB_POSTGRES_INIT=${JUPYTERHUB_POSTGRES_INIT}
        - JUPYTERHUB_POSTGRES_USER=${JUPYTERHUB_POSTGRES_USER}
        - JUPYTERHUB_POSTGRES_PASSWORD=${JUPYTERHUB_POSTGRES_PASSWORD}
        - JUPYTERHUB_POSTGRES_DB=${JUPYTERHUB_POSTGRES_DB}
      networks:
        - backend
.env
#(略)
DB_CONNECTION=pgsql
DB_HOST=postgres
DB_PORT=5432
DB_DATABASE=quickstart
DB_USERNAME=default
DB_PASSWORD=secret
#(略)

migrateの再実行結果

無事、migrateされました。

root@16a77d3a94fe:/var/www/quickstart# php artisan migrate
Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2019_04_10_151437_create_tasks_table
Migrated:  2019_04_10_151437_create_tasks_table

参考

sencorp
幼稚園・保育園向けインターネット写真サービス「はいチーズ!」を提供しています。
https://sencorp.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした