3

Im starting a new laravel project and im trying to configure to run it with laradock postgres container. I have the postgres container running in port 5432 but when i run:

docker-compose exec workspace bash
php artisan migrate

im getting the error:

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

Seens Laravel can't find postgres. I must be missing something.

To build the containers i ran this in my-laravel-app/laradock/:

docker-compose up -d nginx postgres

and i can check everything is up and running fine typing docker-compose ps:

           Name                          Command              State                     Ports
---------------------------------------------------------------------------------------------------------------
laradock_docker-in-docker_1   dockerd-entrypoint.sh           Up       2375/tcp
laradock_mysql_1              docker-entrypoint.sh mysqld     Exit 0
laradock_nginx_1              /bin/bash /opt/startup.sh       Up       0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
laradock_php-fpm_1            docker-php-entrypoint php-fpm   Up       9000/tcp
laradock_postgres_1           docker-entrypoint.sh postgres   Up       0.0.0.0:5432->5432/tcp
laradock_workspace_1          /sbin/my_init                   Up       0.0.0.0:2222->22/tcp

I typed lsof -Pan -i tcp and i can see that postgres container is running in port 5432:

com.docke 693 marcelo   18u  IPv4 0x483d469672d04c2b      0t0  TCP *:5432 (LISTEN)
com.docke 693 marcelo   21u  IPv6 0x483d46967542a923      0t0  TCP [::1]:5432 (LISTEN)

my laradock/.env:

### POSTGRES ##############################################

POSTGRES_DB=sicob_dev
POSTGRES_USER=postgres
POSTGRES_PASSWORD=secret
POSTGRES_PORT=5432
POSTGRES_ENTRYPOINT_INITDB=./postgres/docker-entrypoint-initdb.d

my .env:

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=sicob_database
DB_USERNAME=postgres
DB_PASSWORD=secret

my config/database.php:

# ...
'default' => env('DB_CONNECTION', 'pgsql'),

'connections' => [
    # ...
    'pgsql' => [
        'driver' => 'pgsql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '5432'),
        'database' => env('DB_DATABASE', 'sicob_dev'),
        'username' => env('DB_USERNAME', 'postgres'),
        'password' => env('DB_PASSWORD', 'secret'),
        'charset' => 'utf8',
        'prefix' => '',
        'prefix_indexes' => true,
        'schema' => 'public',
        'sslmode' => 'prefer',
    ],
 ],
 # ...
2
  • I changed 'host' => env('DB_HOST', '127.0.0.1'), to 'host' => env('DB_HOST', 'laradock_postgres_1'), changed to the container name in config/database.php. I think this might work, the error changed. Ill continue working on it Commented Apr 3, 2019 at 18:35
  • did it! i had to change HOSTNAME from 127.0.0.1 to my postgres container name "laradock_postgres_1" and fix my env db_name from sicob_database to sicob_dev. For anyone still working on an answer i would still accept a correct answer Commented Apr 3, 2019 at 19:28

1 Answer 1

4

For future searchers: Your project .env should look like this:

DB_CONNECTION=pgsql
DB_HOST=postgres *(no need to use the full container name, as Laradock exposes 'postgres' as the volume name (check docker-compose.yml))*
DB_PORT=5432
DB_DATABASE=mydb (postgress database you want to connect)
DB_USERNAME=root (postgres user)
DB_PASSWORD=root (postgres password)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.