hanami / cli

Hanami command line
MIT License
27 stars 27 forks source link

Error: Database with the same name as the username does not exist #228

Open patorash opened 2 weeks ago

patorash commented 2 weeks ago

When using hanami-cli 2.1.1 with PostgreSQL, an error occurs when calling create_command. It seems that the error happens when specifying a PostgreSQL user, and there is no database with the same name as the user.

https://github.com/hanami/cli/blob/5bfd953797fcc2ad7b91d3b94c912e017ff070a6/lib/hanami/cli/commands/app/db/utils/postgres.rb#L17

psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL:  database "{{username}}" does not exist
cllns commented 1 week ago

I think this is a quirk of how Postgres infers connections, rather than an issue with Hanami's CLI. See their docs:

DBNAME: Defaults to be the same as the user name.

I think your PR #229 would prevent people from setting PGDATABASE themselves, which we don't want to do.

To clarify, you're specifying a PGUSER, is that right? If so, then you can set PGDATABASE as well. Does that sound like an acceptable solution to you @patorash?

patorash commented 1 week ago

@cllns This process is meant to check if escaped_name exists in the database, right? So, if you specify the same database name as escaped_name in PGDATABASE here, you’ll get an error saying that the database doesn’t exist if it isn’t there. That’s why you need to specify ‘postgres’ as the database name here.

timriley commented 6 days ago

@patorash Thanks for raising this.

Can you please give us reproduction instructions for the error you encountered? I'd like to make sure we're doing the right thing in this circumstance, but I'm not sure that setting PGDATABASE is the way to go, so being able to explore this directly would be helpful.