Different PG user for DB creation

Hi,

I’m testing Hanami 2 and I ran into an issue when creating DB. My PG user is different from my system user and the createdb command returns an error that there is no such user in PG. Can’t I specify this somewhere?

2 Likes

I think it’s a pure postgres question, but you should be able to setup an owner of the db you create using the --owner flag.

Let me know if that answers the question.
For the db settings inside of the hanami app, you just need the URL to your database which can contain user and password specified:

postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]

Source: What is the format for the PostgreSQL connection string / URL? - Stack Overflow

Hi @swilgosz ,

I’m returning to this problem after a while. I’ve checked again and I’ve followed the guides for Hanami 2.1 Web app to the letter and I doubt it’s related to PG.

Ruby 2.7 with Hanami 1.3: db creaton and migrations run fine.
Ruby 3 with Hanami 2.1:

createdb bookshelf_development
createdb: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: FATAL:  role "my_username" does not exist`

The above “my_username” is my Ubuntu user. But it should pick the user from the .env.development file, right. Also, the guides mention the create_db command before the .env files are supposed to be created. Does that make sense?

So why is it picking my system user? The only variables here are Ruby and Hanami versions.

Best, Seba

Hi, I expect that in Hanami 1 you have used Hanami CLI commands to run migrations, which loaded the app and the .env files automatically.

For the snippet above you use createdb directly, which is postgres CLI, having nothing to do with loading .env files nor Hanami app configuration.

If I am wrong, please include exact commands you run for Hanami 1, and for Hanami 2, I will try to reproduce and explain it for you

Hi,

yes, with 1.3 I use bundle exec hanami db prepare for example. I wasn’t aware this has changed. I tried now with createdb and passing my pg role as the argument and it worked.

So this means Hanami no longer supports previous cli commands like prepare and create?

Hanami 2.1 does not have built in persistence CLI yet, but if you will follow my integration for ROM I have published on Hanami Mastery, you will be able to use ROM CLI, like

rake db:create_migration['create_users']

I hope that helps:

Section about rake tasks is here: Configure ROM from scratch | Hanami Mastery

1 Like