avram icon indicating copy to clipboard operation
avram copied to clipboard

Database connection fails for some database operations, but not dev server

Open mraffonso opened this issue 1 year ago • 1 comments

After upgrading to v1.1.0 some lucky commands and running specs fail.

crystal spec produces the following error, which is not correct:

AppDatabase: Failed to connect to database 'app_test' with username 'app'.

Lucky commands such as lucky db.reset provide a complete stack trace which reveals the underlying cause.

Caused by: database "app" does not exist (PQ::PQError)

This behaviour is different to previous lucky versions and may be problematic since it's requiring a database which is not used by the app or delcared in any of the configurations.

Solution:

Create a database with the same name as the PostgreSQL user that the app is using. For example, if the PostgreSQL user is 'app', there should also be a database named 'app' that the 'app' user is the owner of.

createdb --owner app app

mraffonso avatar Nov 03 '23 17:11 mraffonso

I just ran in to this too. This came from removing the pg client tools requirement. Now that we can't just call createdb -U ... directly from the code, we have to connect to a DB and then run the SQL. But postgres seems to have this hard requirement that you need an actual DB in order to run arbitrary SQL.

The connection in this case looks like:

DB.open("postgres://user:pass@host/user") do |db|
  db.exec("CREATE DATABASE ....")
end

Removing that /user causes it to fail with FATAL: database "user" does not exist if you don't have a database by the same name as the user.

I have no clue how to fix this other than maybe trying to catch the error and printing out some helpful error?

jwoertink avatar Dec 28 '23 22:12 jwoertink