craftcms / cms

Build bespoke content experiences with Craft.
https://craftcms.com
Other
3.29k stars 638 forks source link

Default Postgres port not assigned when checking server requirements. #1333

Closed signal-intrusion closed 7 years ago

signal-intrusion commented 7 years ago

Description

When attempting to install Craft with Postgres I get an error: "Can't connect to the database with the credentials supplied in db.php. Please double check them and try again." The credentials are correct. Postgres also logs an error:

ERROR:  relation "sites" does not exist at character 70
STATEMENT:  SELECT "id", "name", "handle", "language", "hasUrls", "baseUrl" FROM "sites" ORDER BY "sortOrder"

If I create the "sites" table myself I get:

ERROR:  column "id" does not exist at character 8
STATEMENT:  SELECT "id", "name", "handle", "language", "hasUrls", "baseUrl" FROM "sites" ORDER BY "sortOrder"

So the problem is that Craft is attempting to query the db before the tables are set up.

So I dug deeper...

When Craft runs it's requirements checks it fails to connect to the Postgres DB. The default postgres port # is not being assigned to RequirementsChecker->dbCreds['port'] right here: https://github.com/craftcms/server-check/blob/develop/server/requirements/RequirementsChecker.php#L408. When I examine the dbCreds['port'] it is an empty string.

If I set 'port' => 5432 in config/db.php everything works.

Steps to reproduce

  1. Install craft with composer
  2. Create a postgres db
  3. Set your .env
  4. Start your db and server
  5. visit <hostname>/admin. It should show the "Can't connect error"
  6. Add 'port' => 5432 to /config/db.php. Refresh. It should continue the installation process.
  7. Remove the port setting. Refresh. It doesn't work again.

Additional info

signal-intrusion commented 7 years ago

Changing to $config->getDbPort() fixes the failing db connection check (https://github.com/craftcms/server-check/pull/2).

The Postgres error still occurs when attempting to register an email because the tables and columns are still not generated on the db.

ERROR:  relation "sites" does not exist at character 70
STATEMENT:  SELECT "id", "name", "handle", "language", "hasUrls", "baseUrl" FROM "sites" ORDER BY "sortOrder"
angrybrad commented 7 years ago

Fixed in https://github.com/craftcms/server-check/releases/tag/1.0.8.

The Postgres error still occurs when attempting to register an email because the tables and columns are still not generated on the db.

I can't reproduce this, though (the installer goes through fine). If it's still an issue for you please open up another issue.

colinwilson commented 7 years ago

I'm seeing this on a fresh craft3beta install (docker), https://github.com/wyveo/craftcms-docker/issues/5.

From the logs it looks like the installer is querying the db for the "sites" table as soon as the install page is loaded?

2017-07-19 07:10:57 [10.0.1.4][-][-][info][yii\db\Connection::open] Opening DB connection: pgsql:host=postgres;dbname=craft3beta;port=5432;
2017-07-19 07:10:57 [10.0.1.4][-][-][profile begin][yii\db\Connection::open] Opening DB connection: pgsql:host=postgres;dbname=craft3beta;port=5432;
2017-07-19 07:10:57 [10.0.1.4][-][-][profile end][yii\db\Connection::open] Opening DB connection: pgsql:host=postgres;dbname=craft3beta;port=5432;
2017-07-19 07:10:57 [10.0.1.4][-][-][info][yii\db\Command::query] SELECT c.relname AS table_name
FROM pg_class c
INNER JOIN pg_namespace ns ON ns.oid = c.relnamespace
WHERE ns.nspname = 'public' AND c.relkind IN ('r','v','m','f')
ORDER BY c.relname
2017-07-19 07:10:57 [10.0.1.4][-][-][profile begin][yii\db\Command::query] SELECT c.relname AS table_name
FROM pg_class c
INNER JOIN pg_namespace ns ON ns.oid = c.relnamespace
WHERE ns.nspname = 'public' AND c.relkind IN ('r','v','m','f')
ORDER BY c.relname
2017-07-19 07:10:57 [10.0.1.4][-][-][profile end][yii\db\Command::query] SELECT c.relname AS table_name
FROM pg_class c
INNER JOIN pg_namespace ns ON ns.oid = c.relnamespace
WHERE ns.nspname = 'public' AND c.relkind IN ('r','v','m','f')
ORDER BY c.relname
2017-07-19 07:10:57 [10.0.1.4][-][-][info][yii\web\Session::open] Session started
2017-07-19 07:10:57 [10.0.1.4][-][-][profile begin][craft\web\View::renderTemplate] _special/install
2017-07-19 07:10:57 [10.0.1.4][-][-][profile begin][craft\web\twig\Environment::compileSource] _special/install
2017-07-19 07:10:57 [10.0.1.4][-][-][profile end][craft\web\twig\Environment::compileSource] _special/install
2017-07-19 07:10:57 [10.0.1.4][-][-][profile begin][craft\web\twig\Environment::compileSource] _layouts/basecp
2017-07-19 07:10:57 [10.0.1.4][-][-][profile end][craft\web\twig\Environment::compileSource] _layouts/basecp
2017-07-19 07:10:57 [10.0.1.4][-][-][profile begin][craft\web\twig\Environment::compileSource] _layouts/base
2017-07-19 07:10:57 [10.0.1.4][-][-][profile end][craft\web\twig\Environment::compileSource] _layouts/base
2017-07-19 07:10:57 [10.0.1.4][-][-][profile begin][craft\web\twig\Template::display] _special/install
2017-07-19 07:10:57 [10.0.1.4][-][-][info][yii\db\Command::query] SELECT "id", "name", "handle", "language", "hasUrls", "baseUrl"
FROM "sites"
ORDER BY "sortOrder"
2017-07-19 07:10:57 [10.0.1.4][-][-][profile begin][yii\db\Command::query] SELECT "id", "name", "handle", "language", "hasUrls", "baseUrl"
FROM "sites"
ORDER BY "sortOrder"
2017-07-19 07:10:57 [10.0.1.4][-][-][profile end][yii\db\Command::query] SELECT "id", "name", "handle", "language", "hasUrls", "baseUrl"
FROM "sites"
ORDER BY "sortOrder"

As you mentioned though this doesn't seem to cause any issues with the installer completing, and I don't see the error again once it does.

brandonkelly commented 7 years ago

@colinwilson Similar error but this one is expected on fresh installs (and Craft will catch its exception). I just cleaned up the Sites service a bit to avoid them anyway, though. (9196dc175b045720c2b8ce0c174165cf510097b9)