Nextcloud can't connect to Postgres via PgBouncer for the first time when it is installed

Support intro

Sorry to hear you’re facing problems :slightly_frowning_face:

help.nextcloud.com is for home/non-enterprise users. If you’re running a business, paid support can be accessed via portal.nextcloud.com where we can ensure your business keeps running smoothly.

In order to help you as quickly as possible, before clicking Create Topic please provide as much of the below as you can. Feel free to use a pastebin service for logs, otherwise either indent short log examples with four spaces:

example

Or for longer, use three backticks above and below the code snippet:

longer
example
here

Some or all of the below information will be requested if it isn’t supplied; for fastest response please provide as much as you can :heart:

Nextcloud version (eg, 20.0.5): 27.1.7
Operating system and version (eg, Ubuntu 20.04): Debian GNU/Linux 12 (bookworm)
Apache or nginx version (eg, Apache 2.4.25): 2.4.57
PHP version (eg, 7.4): 8.2.16

The issue you are facing: Nextcloud can’t connect to Postgres 13 via PgBouncer when install the first time (I install Nextcloud and Postgres in Kubernetes). Then after restart (liveness check fail) Nextcloud can connect to Postgres via PgBouncer and I performed some action like creating folder, add user without error… Meanwhile, in another pod with the same PgBouncer credentials, I connect to Postgres without error.

Below is the log of PgBouncer when first install Nextcloud. It seems Nextcloud send empty or null value of database and username.


After Nextcloud restart because of liveness check fail, Nextcloud send correct db and username to PgBouncer.

Is this the first time you’ve seen this error? (Y/N): Yes.

Steps to replicate it:

  1. Install Postgres 13 using Postgres Operator.
  2. Install Nextcloud 27.1.7 using helm chart.
  3. Nextcloud error when first install:

The output of your Nextcloud log in Admin > Logging:
(sorry for paste text because new user only post with maximun of two media)

Initializing nextcloud 27.1.7.2 ...
2024-03-11T06:50:08.320196763Z New nextcloud instance
2024-03-11T06:50:08.369087370Z Installing with PostgreSQL database
2024-03-11T06:50:08.369279130Z => Searching for scripts (*.sh) to run, located in the folder: /docker-entrypoint-hooks.d/pre-installation
2024-03-11T06:50:08.375500160Z Starting nextcloud installation
2024-03-11T06:50:08.670444911Z PostgreSQL username and/or password not valid
2024-03-11T06:50:08.670492341Z  -> You need to enter details of an existing account.
2024-03-11T06:50:08.670497281Z Trace: #0 /var/www/html/lib/private/Setup.php(356): OC\Setup\PostgreSQL->setupDatabase('nextcloud')
2024-03-11T06:50:08.670501171Z #1 /var/www/html/core/Command/Maintenance/Install.php(115): OC\Setup->install(Array, NULL)
2024-03-11T06:50:08.670505421Z #2 /var/www/html/3rdparty/symfony/console/Command/Command.php(298): OC\Core\Command\Maintenance\Install->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
2024-03-11T06:50:08.670510011Z #3 /var/www/html/3rdparty/symfony/console/Application.php(1040): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 /var/www/html/3rdparty/symfony/console/Application.php(301): Symfony\Component\Console\Application->doRunCommand(Object(OC\Core\Command\Maintenance\Install), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#5 /var/www/html/3rdparty/symfony/console/Application.php(171): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
2024-03-11T06:50:08.670527451Z #6 /var/www/html/lib/private/Console/Application.php(218): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
2024-03-11T06:50:08.670529461Z #7 /var/www/html/console.php(100): OC\Console\Application->run()
2024-03-11T06:50:08.670531841Z #8 /var/www/html/occ(11): require_once('/var/www/html/c...')
2024-03-11T06:50:08.670534381Z #9 {main}
2024-03-11T06:50:08.670536341Z 
2024-03-11T06:50:08.670539821Z Previous: Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[08006] [7] connection to server at "hippo-pgbouncer.postgres-operator.svc" (10.4.10.100), port 5432 failed: could not open certificate file "/root/.postgresql/postgresql.crt": Permission denied
2024-03-11T06:50:08.670543501Z connection to server at "hippo-pgbouncer.postgres-operator.svc" (10.4.10.100), port 5432 failed: FATAL:  SSL required
2024-03-11T06:50:08.670545671Z Trace: #0 /var/www/html/lib/private/Setup/PostgreSQL.php(112): OC\DB\Connection->connect()
2024-03-11T06:50:08.670547661Z #1 /var/www/html/lib/private/Setup.php(356): OC\Setup\PostgreSQL->setupDatabase('nextcloud')
2024-03-11T06:50:08.670549971Z #2 /var/www/html/core/Command/Maintenance/Install.php(115): OC\Setup->install(Array, NULL)
2024-03-11T06:50:08.670552031Z #3 /var/www/html/3rdparty/symfony/console/Command/Command.php(298): OC\Core\Command\Maintenance\Install->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
2024-03-11T06:50:08.670554581Z #4 /var/www/html/3rdparty/symfony/console/Application.php(1040): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput
), Object(Symfony\Component\Console\Output\ConsoleOutput))

The output of your config.php file in /path/to/nextcloud (make sure you remove any identifiable information!):

<?php
$CONFIG = array (
  'trusted_proxies' => 
  array (
    0 => 'localhost',
  ),
  'default_phone_region' => 'VN',
  'overwriteprotocol' => 'https',
  'htaccess.RewriteBase' => '/',
  'memcache.local' => '\\OC\\Memcache\\APCu',
  'apps_paths' => 
  array (
    0 => 
    array (
      'path' => '/var/www/html/apps',
      'url' => '/apps',
      'writable' => false,
    ),
    1 => 
    array (
      'path' => '/var/www/html/custom_apps',
      'url' => '/custom_apps',
      'writable' => true,
    ),
  ),
  'passwordsalt' => 'uAyo2+kpsAIFtlqQPyqkLi+viDE0ld',
  'secret' => '6BORNBMWFboDlmq/xJkFw/wprwxYW18eAoWqx4B5BrEojGn5',
  'trusted_domains' => 
  array (
    0 => 'localhost',
  ),
  'datadirectory' => '/var/www/html/data',
  'dbtype' => 'pgsql',
  'version' => '27.1.7.2',
  'overwrite.cli.url' => 'https://localhost',
  'dbname' => 'nextcloud',
  'dbhost' => 'hippo-pgbouncer.postgres-operator.svc',
  'dbport' => '',
  'dbtableprefix' => 'oc_',
  'dbuser' => 'nextcloud',
  'dbpassword' => 'xxx',
  'instanceid' => 'ocgrzlwfvs93',
);

Output errors in nextcloud.log in /var/www/ or as admin user in top right menu, filtering for errors:

[
	{
		"reqId": "kPzTukXVNDgq8pbM4QiN",
		"level": 2,
		"time": "2024-03-11T06:50:08+00:00",
		"remoteAddr": "",
		"user": "--",
		"app": "no app in context",
		"method": "",
		"url": "--",
		"message": "Error trying to connect as \"postgres\", assuming database is setup and tables need to be created",
		"userAgent": "--",
		"version": "27.1.7.2",
		"exception": {
			"Exception": "Doctrine\\DBAL\\Exception",
			"Message": "Failed to connect to the database: An exception occurred in the driver: SQLSTATE[08006] [7] connection to server at \"hippo-pgbouncer.postgres-operator.svc\" (10.4.10.100), port 5432 failed: could not open certificate file \"/root/.postgresql/postgresql.crt\": Permission denied\nconnection to server at \"hippo-pgbouncer.postgres-operator.svc\" (10.4.10.100), port 5432 failed: FATAL:  SSL required",
			"Code": 7,
			"Trace": [
				{
					"file": "/var/www/html/3rdparty/doctrine/dbal/src/Connection.php",
					"line": 405,
					"function": "connect",
					"class": "OC\\DB\\Connection",
					"type": "->",
					"args": []
				},
				{
					"file": "/var/www/html/3rdparty/doctrine/dbal/src/Connection.php",
					"line": 367,
					"function": "getDatabasePlatformVersion",
					"class": "Doctrine\\DBAL\\Connection",
					"type": "->",
					"args": []
				},
				{
					"file": "/var/www/html/3rdparty/doctrine/dbal/src/Connection.php",
					"line": 284,
					"function": "detectDatabasePlatform",
					"class": "Doctrine\\DBAL\\Connection",
					"type": "->",
					"args": []
				},
				{
					"file": "/var/www/html/lib/private/DB/ConnectionAdapter.php",
					"line": 196,
					"function": "getDatabasePlatform",
					"class": "Doctrine\\DBAL\\Connection",
					"type": "->",
					"args": []
				},
				{
					"file": "/var/www/html/lib/private/DB/QueryBuilder/QueryBuilder.php",
					"line": 121,
					"function": "getDatabasePlatform",
					"class": "OC\\DB\\ConnectionAdapter",
					"type": "->",
					"args": []
				},
				{
					"file": "/var/www/html/lib/private/Setup/PostgreSQL.php",
					"line": 55,
					"function": "expr",
					"class": "OC\\DB\\QueryBuilder\\QueryBuilder",
					"type": "->",
					"args": []
				},
				{
					"file": "/var/www/html/lib/private/Setup.php",
					"line": 356,
					"function": "setupDatabase",
					"class": "OC\\Setup\\PostgreSQL",
					"type": "->",
					"args": [
						"nextcloud"
					]
				},
				{
					"file": "/var/www/html/core/Command/Maintenance/Install.php",
					"line": 115,
					"function": "install",
					"class": "OC\\Setup",
					"type": "->",
					"args": [
						"*** sensitive parameters replaced ***"
					]
				},
				{
					"file": "/var/www/html/3rdparty/symfony/console/Command/Command.php",
					"line": 298,
					"function": "execute",
					"class": "OC\\Core\\Command\\Maintenance\\Install",
					"type": "->",
					"args": [
						[
							"Symfony\\Component\\Console\\Input\\ArgvInput"
						],
						[
							"Symfony\\Component\\Console\\Output\\ConsoleOutput"
						]
					]
				},
				{
					"file": "/var/www/html/3rdparty/symfony/console/Application.php",
					"line": 1040,
					"function": "run",
					"class": "Symfony\\Component\\Console\\Command\\Command",
					"type": "->",
					"args": [
						[
							"Symfony\\Component\\Console\\Input\\ArgvInput"
						],
						[
							"Symfony\\Component\\Console\\Output\\ConsoleOutput"
						]
					]
				},
				{
					"file": "/var/www/html/3rdparty/symfony/console/Application.php",
					"line": 301,
					"function": "doRunCommand",
					"class": "Symfony\\Component\\Console\\Application",
					"type": "->",
					"args": [
						[
							"OC\\Core\\Command\\Maintenance\\Install"
						],
						[
							"Symfony\\Component\\Console\\Input\\ArgvInput"
						],
						[
							"Symfony\\Component\\Console\\Output\\ConsoleOutput"
						]
					]
				},
				{
					"file": "/var/www/html/3rdparty/symfony/console/Application.php",
					"line": 171,
					"function": "doRun",
					"class": "Symfony\\Component\\Console\\Application",
					"type": "->",
					"args": [
						[
							"Symfony\\Component\\Console\\Input\\ArgvInput"
						],
						[
							"Symfony\\Component\\Console\\Output\\ConsoleOutput"
						]
					]
				},
				{
					"file": "/var/www/html/lib/private/Console/Application.php",
					"line": 218,
					"function": "run",
					"class": "Symfony\\Component\\Console\\Application",
					"type": "->",
					"args": [
						[
							"Symfony\\Component\\Console\\Input\\ArgvInput"
						],
						[
							"Symfony\\Component\\Console\\Output\\ConsoleOutput"
						]
					]
				},
				{
					"file": "/var/www/html/console.php",
					"line": 100,
					"function": "run",
					"class": "OC\\Console\\Application",
					"type": "->",
					"args": []
				},
				{
					"file": "/var/www/html/occ",
					"line": 11,
					"args": [
						"/var/www/html/console.php"
					],
					"function": "require_once"
				}
			],
			"File": "/var/www/html/lib/private/DB/Connection.php",
			"Line": 140,
			"message": "Error trying to connect as \"postgres\", assuming database is setup and tables need to be created",
			"exception": {},
			"CustomMessage": "Error trying to connect as \"postgres\", assuming database is setup and tables need to be created"
		}
	},
	{
		"reqId": "kPzTukXVNDgq8pbM4QiN",
		"level": 3,
		"time": "2024-03-11T06:50:08+00:00",
		"remoteAddr": "",
		"user": "--",
		"app": "no app in context",
		"method": "",
		"url": "--",
		"message": "Failed to connect to the database: An exception occurred in the driver: SQLSTATE[08006] [7] connection to server at \"hippo-pgbouncer.postgres-operator.svc\" (10.4.10.100), port 5432 failed: could not open certificate file \"/root/.postgresql/postgresql.crt\": Permission denied\nconnection to server at \"hippo-pgbouncer.postgres-operator.svc\" (10.4.10.100), port 5432 failed: FATAL:  SSL required",
		"userAgent": "--",
		"version": "27.1.7.2",
		"exception": {
			"Exception": "Doctrine\\DBAL\\Exception",
			"Message": "Failed to connect to the database: An exception occurred in the driver: SQLSTATE[08006] [7] connection to server at \"hippo-pgbouncer.postgres-operator.svc\" (10.4.10.100), port 5432 failed: could not open certificate file \"/root/.postgresql/postgresql.crt\": Permission denied\nconnection to server at \"hippo-pgbouncer.postgres-operator.svc\" (10.4.10.100), port 5432 failed: FATAL:  SSL required",
			"Code": 7,
			"Trace": [
				{
					"file": "/var/www/html/lib/private/Setup/PostgreSQL.php",
					"line": 112,
					"function": "connect",
					"class": "OC\\DB\\Connection",
					"type": "->",
					"args": []
				},
				{
					"file": "/var/www/html/lib/private/Setup.php",
					"line": 356,
					"function": "setupDatabase",
					"class": "OC\\Setup\\PostgreSQL",
					"type": "->",
					"args": [
						"nextcloud"
					]
				},
				{
					"file": "/var/www/html/core/Command/Maintenance/Install.php",
					"line": 115,
					"function": "install",
					"class": "OC\\Setup",
					"type": "->",
					"args": [
						"*** sensitive parameters replaced ***"
					]
				},
				{
					"file": "/var/www/html/3rdparty/symfony/console/Command/Command.php",
					"line": 298,
					"function": "execute",
					"class": "OC\\Core\\Command\\Maintenance\\Install",
					"type": "->",
					"args": [
						[
							"Symfony\\Component\\Console\\Input\\ArgvInput"
						],
						[
							"Symfony\\Component\\Console\\Output\\ConsoleOutput"
						]
					]
				},
				{
					"file": "/var/www/html/3rdparty/symfony/console/Application.php",
					"line": 1040,
					"function": "run",
					"class": "Symfony\\Component\\Console\\Command\\Command",
					"type": "->",
					"args": [
						[
							"Symfony\\Component\\Console\\Input\\ArgvInput"
						],
						[
							"Symfony\\Component\\Console\\Output\\ConsoleOutput"
						]
					]
				},
				{
					"file": "/var/www/html/3rdparty/symfony/console/Application.php",
					"line": 301,
					"function": "doRunCommand",
					"class": "Symfony\\Component\\Console\\Application",
					"type": "->",
					"args": [
						[
							"OC\\Core\\Command\\Maintenance\\Install"
						],
						[
							"Symfony\\Component\\Console\\Input\\ArgvInput"
						],
						[
							"Symfony\\Component\\Console\\Output\\ConsoleOutput"
						]
					]
				},
				{
					"file": "/var/www/html/3rdparty/symfony/console/Application.php",
					"line": 171,
					"function": "doRun",
					"class": "Symfony\\Component\\Console\\Application",
					"type": "->",
					"args": [
						[
							"Symfony\\Component\\Console\\Input\\ArgvInput"
						],
						[
							"Symfony\\Component\\Console\\Output\\ConsoleOutput"
						]
					]
				},
				{
					"file": "/var/www/html/lib/private/Console/Application.php",
					"line": 218,
					"function": "run",
					"class": "Symfony\\Component\\Console\\Application",
					"type": "->",
					"args": [
						[
							"Symfony\\Component\\Console\\Input\\ArgvInput"
						],
						[
							"Symfony\\Component\\Console\\Output\\ConsoleOutput"
						]
					]
				},
				{
					"file": "/var/www/html/console.php",
					"line": 100,
					"function": "run",
					"class": "OC\\Console\\Application",
					"type": "->",
					"args": []
				},
				{
					"file": "/var/www/html/occ",
					"line": 11,
					"args": [
						"/var/www/html/console.php"
					],
					"function": "require_once"
				}
			],
			"File": "/var/www/html/lib/private/DB/Connection.php",
			"Line": 140,
			"message": "Failed to connect to the database: An exception occurred in the driver: SQLSTATE[08006] [7] connection to server at \"hippo-pgbouncer.postgres-operator.svc\" (10.4.10.100), port 5432 failed: could not open certificate file \"/root/.postgresql/postgresql.crt\": Permission denied\nconnection to server at \"hippo-pgbouncer.postgres-operator.svc\" (10.4.10.100), port 5432 failed: FATAL:  SSL required",
			"exception": {},
			"CustomMessage": "Failed to connect to the database: An exception occurred in the driver: SQLSTATE[08006] [7] connection to server at \"hippo-pgbouncer.postgres-operator.svc\" (10.4.10.100), port 5432 failed: could not open certificate file \"/root/.postgresql/postgresql.crt\": Permission denied\nconnection to server at \"hippo-pgbouncer.postgres-operator.svc\" (10.4.10.100), port 5432 failed: FATAL:  SSL required"
		}
	}
]

Thank you for reading my question!

We managed to get over it by adding HOME env to our Helm. This happens because HOME env is set to /home/root. And our PSQL CA sertificate are in /usr/local/share/ca-certificates/

extraEnv:
- name: HOME
value: /usr/local/share/ca-certificates/

But I belive, if you do not use TLS you can set this path to somewhere else, like /tmp/ :smiley:

I did get this idea from: