Marcelo Fonseca
Marcelo Fonseca

Reputation: 1844

how to configure laravel with postgres using laradock? could not connect to server: Connection refused on port 5432

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',
    ],
 ],
 # ...

Upvotes: 3

Views: 8905

Answers (1)

Thiago Meireles
Thiago Meireles

Reputation: 171

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)

Upvotes: 4

Related Questions