-
-
Notifications
You must be signed in to change notification settings - Fork 33
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
Error: Database with the same name as the username does not exist #228
Comments
I think this is a quirk of how Postgres infers connections, rather than an issue with Hanami's CLI. See their docs:
I think your PR #229 would prevent people from setting To clarify, you're specifying a |
@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. |
@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 |
Normally, this always happens when there is no database with the same name as the username. Probably, most users do not encounter this error because they use “postgres” as the username during development. This is because a database named “postgres” always exists. Therefore, we set the PGDATABASE only at the timing of executing the psql command on line 17. I apologize if this is due to my lack of knowledge, but if you know a way to avoid this issue when there is no database with the same name as the username, please let me know. |
Hi @patorash, I've just found the time to look into this. Here's how I tested it. I installed postgres via Homebrew, to replicate a common installation approach on macOS:
Then I ran postgres manually, according to the instructions in the post-installation message: $ /opt/homebrew/opt/postgresql@14/bin/postgres -D /opt/homebrew/var/postgresql@14
2024-10-28 20:42:27.508 AEDT [22959] LOG: starting PostgreSQL 14.13 (Homebrew) on aarch64-apple-darwin23.4.0, compiled by Apple clang version 15.0.0 (clang-1500.3.9.4), 64-bit
2024-10-28 20:42:27.509 AEDT [22959] LOG: listening on IPv4 address "127.0.0.1", port 5432
2024-10-28 20:42:27.509 AEDT [22959] LOG: listening on IPv6 address "::1", port 5432
2024-10-28 20:42:27.510 AEDT [22959] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2024-10-28 20:42:27.515 AEDT [22960] LOG: database system was shut down at 2024-10-28 20:35:04 AEDT
2024-10-28 20:42:27.519 AEDT [22959] LOG: database system is ready to accept connections Trying to connect, you can see that there is no database matching my username: $ psql
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "tim" does not exist After this, I generated a new Hanami app, created a migration, migrated, connected to the database and created a record successfully (output below skipped where it is not relevant): $ hanami new elephant --database=postgres
$ cat .env
# This is checked into source control, so put sensitive values into `.env.local`
DATABASE_URL=postgres://localhost/elephant
$ be hanami g migration create_posts
$ cat config/db/migrate/20241028204329_create_posts.rb
# frozen_string_literal: true
ROM::SQL.migration do
# Add your migration here.
#
# See https://sequel.jeremyevans.net/rdoc/files/doc/migration_rdoc.html for details.
change do
create_table :posts do
column :title, :text
end
end
end
$ be hanami db prepare
=> database elephant created
=> database elephant migrated in 0.0028s
=> elephant structure dumped to config/db/structure.sql in 0.0353s
$ cat config/db/structure.sql | grep posts
-- Name: posts; Type: TABLE; Schema: public; Owner: -
CREATE TABLE public.posts (
('20241028204329_create_posts.rb');
$ be hanami g relation posts
$ be hanami c
elephant[development]> app["relations.posts"]
=> #<Elephant::Relations::Posts name=ROM::Relation::Name(posts) dataset=#<Sequel::Postgres::Dataset: "SELECT \"posts\".\"title\" FROM \"posts\"">>
elephant[development]> app["relations.posts"].insert(title: "test")
elephant[development]> app["relations.posts"].to_a
=> [{:title=>"test"}] So everything works as expected. Since I can't replicate your issue, so I'm reluctant to merge the PR you've proposed, at least not right at this minute. Could you please help supply a replication? Thank you 🙏🏼 |
@timriley First, I tried to delete the DB (it shows as Dropped, but it is not actually deleted) vscode ➜ /workspaces/******** (chore/hanami-update) $ hanami db drop
=> database ********_development dropped
=> database ********_test dropped If it is deleted, you should be able to create it, but you will get an error. vscode ➜ /workspaces/******** (chore/hanami-update) $ hanami db create
=> failed to create database ********_development
createdb: error: database creation failed: ERROR: database “********_development” already exists The reason is that in this line, as previously reported, the database with the same name as the user name does not exist. If you omit the database name, it will try to connect to the database with the same name as the user name.
As a result, the result of However, since the result is true, we see
This happens when the same database as the user name does not exist. To work around this, I think you need to create a database in advance that is the same as the user name, or specify a database that always exists, such as postgres. Translated with DeepL.com (free version) |
To summarize the above, there is a bug where the database recreation fails if a database with the same name as the username does not exist. |
Hi @patorash, can you provide a reproducible list of instructions for us to replicate this, from an empty/default postgres install? |
@cllns @timriley
sudo apt update && sudo apt full-upgrade -y
sudo apt install postgresql postgresql-contrib libpq-dev
sudo systemctl start postgresql
sudo systemctl enable postgresql
$ sudo su postgres
$ psql
psql (16.4 (Ubuntu 16.4-0ubuntu0.24.04.2))
Type "help" for help.
postgres=#
sudo apt-get install autoconf patch build-essential rustc libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libgmp-dev libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev uuid-dev
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
~/.rbenv/bin/rbenv init
source ~/.bash_profile
rbenv install 3.3.6
rbenv global 3.3.6
sudo apt install nodejs npm
gem install hanami
mkdir sources
cd sources
hanami new elephant --database=postgres
$ cat .env
# This is checked into source control, so put sensitive values into `.env.local`
DATABASE_URL=postgres://localhost/elephant
$ sudo su postgres
$ createuser -d -P tim
Enter password for new role: ********
Enter it again: ********
$ bundle exec hanami db create
=> database elephant created
=> database elephant_test created
$ bundle exec hanami db drop
=> database elephant dropped
=> database elephant_test dropped
$ bundle exec hanami db create
=> failed to create database elephant
createdb: error: database creation failed: ERROR: database "elephant" already exists |
@patorash I can reproduce it. From my investigation, the problem is with I wonder why Hanami uses |
@katafrakt Thank you for your confirmation! Since the issue is being discussed elsewhere, I will wait for the conclusion from that discussion. |
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.
cli/lib/hanami/cli/commands/app/db/utils/postgres.rb
Line 17 in 5bfd953
The text was updated successfully, but these errors were encountered: