Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Silent crash when initializing a PDO connection to PostgreSQL #174

Closed
tristanjahier opened this issue May 13, 2024 · 8 comments
Closed

Silent crash when initializing a PDO connection to PostgreSQL #174

tristanjahier opened this issue May 13, 2024 · 8 comments

Comments

@tristanjahier
Copy link

tristanjahier commented May 13, 2024

Tinker Version

2.9.0

Laravel Version

10.48.9

PHP Version

8.3.7

Database Driver & Version

PostgreSQL 16 for macOS 14 (homebrew)

Description

I am having troubles since months when trying to access my PostgreSQL database from tinker (Laravel). The interactive shell crashes silently (no standard output, exit status code is 0) when I run this code:

$pdo = new PDO('pgsql:host=127.0.0.1;port=5432;dbname=toto', 'myusername', 'mypassword');

I also tried the same code with PHP's native interactive shell, php -a, and it runs fine.

When working with a MySQL database, it is fine.

MacOS Sonoma 14.4.1
PHP 8.3.*
PostgreSQL 16

I am lost as I do not know how to debug this without any error output. :/

Steps To Reproduce

Start php artisan tinker and run this code with a correctly set up PostgreSQL database and valid credentials:

$pdo = new PDO('pgsql:host=127.0.0.1;port=5432;dbname=toto', 'myusername', 'mypassword');
@mfn
Copy link

mfn commented May 14, 2024

Works for me:

# ./artisan tinker
Psy Shell v0.12.3 (PHP 8.3.4 — cli) by Justin Hileman
> $pdo = new PDO('pgsql:host=pgsql;port=5432;dbname=db', 'user', 'pass');
= PDO {#8460
    inTransaction: false,
    attributes: {
      CASE: NATURAL,
      ERRMODE: EXCEPTION,
      PERSISTENT: false,
      DRIVER_NAME: "pgsql",
      SERVER_INFO: "PID: 226; Client Encoding: UTF8; Is Superuser: on; Session Authorization: db; Date Style: ISO, MDY",
      ORACLE_NULLS: NATURAL,
      CLIENT_VERSION: "13.14",
      SERVER_VERSION: "16.2 (Debian 16.2-1.pgdg120+2)",
      STATEMENT_CLASS: [
        "PDOStatement",
      ],
      EMULATE_PREPARES: false,
      CONNECTION_STATUS: "Connection OK; waiting to send.",
      STRINGIFY_FETCHES: false,
      DEFAULT_FETCH_MODE: BOTH,
    },
  }

pgsql 16 in a docker container, not homebrew

@driesvints
Copy link
Member

Thanks @mfn

@bobthecow
Copy link
Contributor

@tristanjahier if you run tinker with the -vvv flag do you get more information?

@tristanjahier
Copy link
Author

tristanjahier commented May 14, 2024

@tristanjahier if you run tinker with the -vvv flag do you get more information?

I get this output in gray:

return $pdo = new PDO('pgsql:host=127.0.0.1;port=5432;dbname=toto', 'myusername', 'mypassword');

and nothing else.

If the PostgreSQL server is offline, I simply get a normal error when constructing the PDO instance:

PDOException SQLSTATE[08006] [7] connection to server at "127.0.0.1", port 5432 failed: Connection refused
Is the server running on that host and accepting TCP/IP connections?

@bobthecow
Copy link
Contributor

if you disable process forking (i.e. add 'usePcntl' => false to your PsySH config) does it work?

@tristanjahier
Copy link
Author

tristanjahier commented May 14, 2024

Turning this feature off makes things work again. 🙂👍🏻 Thank you a lot!

I had no problem on my previous computer (with the same PsySH config). The PCNTL extension is enabled. What does it tell? Do I miss a library?

@bobthecow
Copy link
Contributor

you have reached a fork in the road. you can dig in, to figure out why it's failing, or you can take the win and walk away :P

if it works with process forking off, and doesn't work with it on, it's probably not a missing library. in the past issues like this have been due to bugs in php extensions, linked libraries, or PHP bindings to libraries.

the next step is probably to come up with the smallest piece of code that can reproduce it. i'd guess it's probably something like:

  1. open a Postgres PDO connection
  2. fork
  3. (maybe) use your connection to make a query?

@tristanjahier
Copy link
Author

Thank you for your detailed answer @bobthecow! Unfortunately, I think that playing with process forks is a bit out of my skill set. :(

However, if I ever come to spend time on this problem again, I will open an issue or a PR on PsySH's repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants