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

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

Open
mraffonso opened this issue Nov 3, 2023 · 1 comment
Labels
help wanted Additional expertise needed

Comments

@mraffonso
Copy link

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
@jwoertink
Copy link
Member

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 jwoertink added the help wanted Additional expertise needed label Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Additional expertise needed
Projects
None yet
Development

No branches or pull requests

2 participants