mix ecto.create and passwordless postgres

Hi, I wanted to post about this here before making an issue on Github since I’m not sure it’s a user error or a real bug. I just installed postgres and by default it created a postgres user without a password while the default code generated by phx.new (in config/dev.exs) does have a password. That isn’t the issue. The issue was what happened when I tried to make the code reflect the lack of password. I tried setting it to an empty string and also to nil but in both cases I got the same incorrect password error from postgres when I ran mix ecto.create. When I changed my postgres user password to “postgres”, though, I was able to run ecto.create successfully. Wondering if ecto simply doesn’t handle the no password case currently? Is that by design?

It does, there’s a couple threads in the past about it, the thing is that postgres need to be configured to accept other ways of connecting to it and by default (at least in Fedora, YMMV) passwordless connections (in this case peer auth) are only available to unix domain socket connections.

I’m assuming that you would like to do this for dev purposes and not for production (passwordless auth in production can be safe, but it isn’t easy), and the easiest way to achieve it is to pass :socket_dir to the config at dev.exs.

config :tambor, MyApp.Repo,
  socket_dir: "/var/run/postgresql",
  database: "myapp_db",
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

Should be enough to make Ecto connect to the DB.

Just now I tried altering my postgres user to have no password with

sudo -u postgres psql -c "ALTER USER postgres PASSWORD null;"

Then changed my config to:

# Configure your database
config :my_app, MyApp.Repo,
  socket_dir: "/var/run/postgresql",
  username: "postgres",
  hostname: "localhost",
  database: "my_app_dev",
  stacktrace: true,
  show_sensitive_data_on_connection_error: true,
  pool_size: 10

but still getting an invalid password error from postgres. Not sure I’m doing this right, not very experienced with postgres or elixir. Anyway thanks for the help!

You can also try connecting with the connection string:

config :my_app, MyApp.Repo,
  url: "..."

Haven’t checked it, but I’m assuming that Postgres will interpret the connection string correctly.