Answers.Body: <p>The most common cause of differences between HTTP and CLI behavior is their execution contexts.</p>
  <p>If you are using DDEV or another containerized development tool, you are likely browsing the control panel and front-end via a URL like <code>https://my-project.ddev.site</code>. Those requests are routed to and processed within a container, where the database host (i.e. <code>db</code>) resolves properly. <em>Outside</em> that container (like from the shell on your host machine), <code>db</code> will <em>not</em> be a valid hostname for the project’s database server, so Craft will be unable to connect.</p>
  <p>Instead of executing <code>./craft</code> directly on your machine, run it within the container! That might mean something like these:</p>
  <pre class="lang-bash prettyprint-override"><code># DDEV:
  ddev craft help
  
  # Docker:
  docker-compose exec web php craft help
  </code></pre>
  <p>Even without a containerized development environment, it's possible that HTTP and CLI requests are using two different versions of PHP that could introduce subtle and unpredictable errors.</p>
  <p>Let us know a bit about your development environment, and I can update this with more specific info!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-07T12:46:20.910Z
Answers.Text: Thanks August, but we are on a live system, not in DDEV. No containers, nothing special. That’s why I am surprised.
Answers.UserDisplayName: Matthias Redl-Mann
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-05T21:00:07.813Z
Answers.LastActivityDate: 2023-12-05T21:00:07.813Z
Answers.UserDisplayName: August Miller
Body: <p>We just completed an upgrade from Craft 3 to 4 (latest version) and now all web requests (FE+CP) work, but not the console commands, it can not connect to the database</p>
  <pre><code>./craft users/list-admins
  
      Craft can’t connect to the database. Check your connection settings.  
  </code></pre>
  <ul>
  <li>config/db.php exists using .env constants</li>
  <li>.env exists but does not user CRAFT_DB_* prefixed ENV constants yet (works in another project that way)</li>
  <li>The content of the console command's file &quot;craft&quot; is identical to another Craft 4 project, where the console command works.</li>
  </ul>
  <p>Any ideas what to check?</p>
  
Tags: <database><craft4><console>
Title: Craft 4 DB connection works in WEB context but not from Craft's console command
ThreadText: # Craft 4 DB connection works in WEB context but not from Craft's console command
  
  (Asked by: username_0 on 2023-12-05)
  
  We just completed an upgrade from Craft 3 to 4 (latest version) and now all web requests (FE+CP) work, but not the console commands, it can not connect to the database
  
  ```
  ./craft users/list-admins
  
      Craft can’t connect to the database. Check your connection settings.  
  
  ```
  
  * config/db.php exists using .env constants
  * .env exists but does not user CRAFT\_DB\_\* prefixed ENV constants yet (works in another project that way)
  * The content of the console command's file "craft" is identical to another Craft 4 project, where the console command works.
  
  Any ideas what to check?
  
  **Tags:** database, craft4, console
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-12-05. Score: 2
  
  The most common cause of differences between HTTP and CLI behavior is their execution contexts.
  
  If you are using DDEV or another containerized development tool, you are likely browsing the control panel and front-end via a URL like `https://my-project.ddev.site`. Those requests are routed to and processed within a container, where the database host (i.e. `db`) resolves properly. *Outside* that container (like from the shell on your host machine), `db` will *not* be a valid hostname for the project’s database server, so Craft will be unable to connect.
  
  Instead of executing `./craft` directly on your machine, run it within the container! That might mean something like these:
  
  ```bash
  # DDEV:
  ddev craft help
  
  # Docker:
  docker-compose exec web php craft help
  
  ```
  
  Even without a containerized development environment, it's possible that HTTP and CLI requests are using two different versions of PHP that could introduce subtle and unpredictable errors.
  
  Let us know a bit about your development environment, and I can update this with more specific info!
  
  > **Comments:**
  > **username_0**: Thanks username_1, but we are on a live system, not in DDEV. No containers, nothing special. That’s why I am surprised.  
  
  ---
  
  