teslamate-org / teslamate

A self-hosted data logger for your Tesla 🚘
https://docs.teslamate.org
MIT License
6k stars 748 forks source link

[dev] Unable to establish code space; postgres user / password not established #4032

Open cwanja opened 4 months ago

cwanja commented 4 months ago

Is there an existing issue for this?

What happened?

Not necessarily a TeslaMate issue, but in light of #4007, I stood up a GitHub code space in my forked repo. However, when following the documentation I cannot get mix setup or MIX_ENV=test mix ecto.setup to run.

I would have expected the password in the runtime.exs to establish the database and authentication. But according to the PSQL logs (below), there is no password set.

Expected Behavior

Codespace is compile and establish app.

Steps To Reproduce

  1. Create GitHub code space
  2. Setup environment
  3. Run commands following documentation

Relevant log output

01:06:51.898 [error] Postgrex.Protocol (#PID<0.9217.0>) failed to connect: ** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"

01:06:51.901 [error] Failure while translating Erlang's logger event
** (MatchError) no match of right hand side value: %{label: {:gen_statem, :terminate}, name: #PID<0.9217.0>, reason: {:error, %Postgrex.Error{message: nil, postgres: %{code: :invalid_password, line: "323", message: "password authentication failed for user \"postgres\"", file: "auth.c", unknown: "FATAL", severity: "FATAL", pg_code: "28P01", routine: "auth_failed"}, connection_id: nil, query: nil}, [{DBConnection.Connection, :handle_event, 4, [file: ~c"lib/db_connection/connection.ex", line: 122, error_info: %{module: Exception}]}, {:gen_statem, :loop_state_callback, 11, [file: ~c"gen_statem.erl", line: 3115]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 329]}]}, log: [], state: Postgrex.Protocol, queue: [internal: {:connect, :init}], modules: [DBConnection.Connection], process_label: :undefined, client_info: :undefined, callback_mode: &DBConnection.Connection.handle_event/4, postponed: [], timeouts: {0, []}, state_enter: false}
    (logger 1.17.1) lib/logger/translator.ex:306: Logger.Translator.report_gen_statem_terminate/2
    (logger 1.17.1) lib/logger/utils.ex:47: Logger.Utils.translate/5
    (logger 1.17.1) lib/logger/utils.ex:25: Logger.Utils.translator/2
    (kernel 10.0) logger_backend.erl:102: :logger_backend.do_apply_filters/4
    (kernel 10.0) logger_backend.erl:87: :logger_backend.apply_filters/4
    (kernel 10.0) logger_backend.erl:33: :logger_backend.log_allowed/3
    (stdlib 6.0) gen_statem.erl:4309: :gen_statem.terminate/7
    (stdlib 6.0) proc_lib.erl:329: :proc_lib.init_p_do_apply/3

01:06:51.913 [error] Postgrex.Protocol (#PID<0.9224.0>) failed to connect: ** (Postgrex.Error) FATAL 28P01 (invalid_password) password authentication failed for user "postgres"

01:06:51.913 [error] Failure while translating Erlang's logger event
** (MatchError) no match of right hand side value: %{label: {:gen_statem, :terminate}, name: #PID<0.9224.0>, reason: {:error, %Postgrex.Error{message: nil, postgres: %{code: :invalid_password, line: "323", message: "password authentication failed for user \"postgres\"", file: "auth.c", unknown: "FATAL", severity: "FATAL", pg_code: "28P01", routine: "auth_failed"}, connection_id: nil, query: nil}, [{DBConnection.Connection, :handle_event, 4, [file: ~c"lib/db_connection/connection.ex", line: 122, error_info: %{module: Exception}]}, {:gen_statem, :loop_state_callback, 11, [file: ~c"gen_statem.erl", line: 3115]}, {:proc_lib, :init_p_do_apply, 3, [file: ~c"proc_lib.erl", line: 329]}]}, log: [], state: Postgrex.Protocol, queue: [internal: {:connect, :init}], modules: [DBConnection.Connection], process_label: :undefined, client_info: :undefined, callback_mode: &DBConnection.Connection.handle_event/4, postponed: [], timeouts: {0, []}, state_enter: false}
    (logger 1.17.1) lib/logger/translator.ex:306: Logger.Translator.report_gen_statem_terminate/2
    (logger 1.17.1) lib/logger/utils.ex:47: Logger.Utils.translate/5
    (logger 1.17.1) lib/logger/utils.ex:25: Logger.Utils.translator/2
    (kernel 10.0) logger_backend.erl:102: :logger_backend.do_apply_filters/4
    (kernel 10.0) logger_backend.erl:87: :logger_backend.apply_filters/4
    (kernel 10.0) logger_backend.erl:33: :logger_backend.log_allowed/3
    (stdlib 6.0) gen_statem.erl:4309: :gen_statem.terminate/7
    (stdlib 6.0) proc_lib.erl:329: :proc_lib.init_p_do_apply/3

** (Mix) The database for TeslaMate.Repo couldn't be created: killed

From the PSQL log:

2024-07-04 01:09:19.585 UTC [16112] postgres@teslamate_dev FATAL:  password authentication failed for user "postgres"
2024-07-04 01:09:19.585 UTC [16112] postgres@teslamate_dev DETAIL:  User "postgres" has no password assigned.
        Connection matched file "/etc/postgresql/16/main/pg_hba.conf" line 125: "host    all             all             127.0.0.1/32            scram-sha-256"

Screenshots

No response

Additional data

No response

Type of installation

GitHub Codespaces

Version

v1.29.X

JakobLichterfeld commented 4 months ago

Thanks for reporting.

But according to the PSQL logs (below), there is no password set.

Not sure if I get this right, but I do understand, that TeslaMate could not know your Postgres Password in dev environment. https://docs.teslamate.org/docs/development is only the TeslaMate part, not Mosquitto nor Postgres. Brian created a nix flake to spin up a whole dev environment #3485 and #3760

cwanja commented 4 months ago

But the mix setup and MIX_ENV=test mix ecto.setup should establish the database. But it does not work.

Would be curious if others could get a GitHub code space to work? It's free for a certain number of hours.

JakobLichterfeld commented 4 months ago

Will give it a try in the next weeks

github-actions[bot] commented 3 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

cwanja commented 3 months ago

@JakobLichterfeld have you had a chance to look at this? Would love to leverage this as I do not have a spare development environment. And this seems like a great way to spin up and down a developer instance. Thanks!

brianmay commented 3 months ago

I am not sure if I understand this issue.

"mix ecto.setup" is just an an alias fro "mix ecto.create" and "mix ecto.migrate".

https://github.com/teslamate-org/teslamate/blob/master/mix.exs#L77

mix create will connect to an existing postgresql server, using connection configuration (username, password, host, etc) that has been already been provided and issue a "CREATE DATABASE" command.

i.e. you must already have a connection to the database server in order to run "mix ecto.create".

Apologies if I misunderstood.

cwanja commented 3 months ago

Guess I am confused. And fully admitting mix and all of this is well outside my wheelhouse. Spent way longer than I would like to admit to trying to get it setup.

Our documentation says a database already needs to be created:

To run the TeslaMate test suite you need a database named teslamate_test:

Yet you are saying the mix create as part of mix ecto.setup will issue a create database command.

mix create will connect to an existing postgresql server, using connection configuration (username, password, host, etc) that has been already been provided and issue a "CREATE DATABASE" command.

brianmay commented 3 months ago

The test database is a special case, that should be created automatically if it doesn't exist.

The mix create command is for the non-test database.

cwanja commented 3 months ago

I am still confused. Are you saying I need to establish a database, user and password that matches the mix commands?

And if I do not need to do that because the mix commands take care of it, my issue is then that the commands do not work given the errors above.

I'd be really curious if anyone can setup a Codespace and get it working (it's free minus your time). It's entirely possible that the commands work (I am sure they do) and my environment is missing something.

JakobLichterfeld commented 3 months ago

@JakobLichterfeld have you had a chance to look at this? Would love to leverage this as I do not have a spare development environment. And this seems like a great way to spin up and down a developer instance. Thanks!

Sorry not yet

brianmay commented 3 months ago

Like I said, mix ecto.setup is just a wrapper function that sends certain commands to the database for convenience.

You are expected to setup the database server and have authentication work yourself.

JakobLichterfeld commented 3 months ago

Or use the nix flake to set up dev environment, innit?

brianmay commented 3 months ago

Yes, that should work also. If the dev environment is active (I have this setup using direnv), then devenv up will bring up all the required dependencies. And the environment variables should be configured, so commands like "mix setup" will just work.

github-actions[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 1 month ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

github-actions[bot] commented 4 days ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.