10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-04-10

はじめに

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

参考

10
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?