PostgreSQL connection via environment variables for local and production environments.

Posted 8 months ago by ElijahPaul

How would i configure the config/database.php file to account for an unsecured local PostgreSQL connection and a secure production one?

My current config:

'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',

If I add the following to the database.php config file...

'sslcert' => env('DB_SSLCERT', ''),
'sslkey' => env('DB_SSLKEY', ''),
'sslrootcert' => env('DB_SSLROOTCERT', ''),

And set the environment variables (only in production)...


The local connection (without adding the corresponding environment variables) fails, but the production environment (with the variables set) connects successfully.

So how should i configure config/database.php to accommodate both local (unsecured) and production (secure) database connection requirements?

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.