It sounds like your containerized Laravel app is unable to connect to the external Postgres server to store sessions. Since switching to SESSION_DRIVER=file works, the core app is fine—it's specifically the database connectivity for session storage that's the problem.
Here’s a step-by-step checklist and some common solutions:
1. Check Your .env Configuration
Ensure your .env has the correct connection details for Postgres:
SESSION_DRIVER=database
DB_CONNECTION=pgsql
DB_HOST=<host_ip_or_name>
DB_PORT=5432
DB_DATABASE=<your_db>
DB_USERNAME=<your_user>
DB_PASSWORD=<your_password>
- Make sure
<host_ip_or_name>isn’tlocalhost(inside Docker,localhostrefers to the container itself). - Use the host machine’s IP or a Docker network name if applicable.
2. Enable Remote Connections in postgresql.conf
Open your postgresql.conf and set:
listen_addresses = '*'
This allows Postgres to accept connections from non-local addresses.
3. Configure pg_hba.conf Properly
You need to allow connections from the container’s IP/network. Add a line like:
host your_db your_user 0.0.0.0/0 md5
Or, to be more secure, limit to your Docker bridge network IP range (often something like 172.17.0.0/16):
host your_db your_user 172.17.0.0/16 md5
After this, reload/restart Postgres.
4. Ensure Network Accessibility
- Make sure the Postgres port (
5432) is open on your host’s firewall. - From inside the container, test connectivity with:
psql -h <host_ip_or_name> -U <your_user> <your_db>
Install psql in the container if needed.
5. Rebuild Migration
Ensure your sessions table exists in your database:
php artisan session:table
php artisan migrate
6. Check Laravel Logs
Look in storage/logs/laravel.log for any connection errors—they’ll indicate if authentication, host, or network problems exist.
Summary:
It’s almost always a networking or Postgres configuration problem when this occurs in a containerized setup. Double-check the .env variables, update pg_hba.conf for external connections (based on your docker network), and confirm your firewall rules.
If possible, post any error message from Laravel logs or Postgres logs if you’re still stuck!