Ubuntu 22.04 postgres setup without password same as how OSx has it
when you develop Rails app on OSx Postgresql it's default that user don't need password to create DB so your database.yml may look like
default: &default adapter: postgresql encoding: unicode pool: 5 development: <<: *default database: myapp_development host: localhost
..so no user no pass
Problem is that Ubuntu Postgres (installed with standard $sudo apt-get install postgresql postgresql-contrib libpq-dev )
Problem is that Ubuntu Postgres (installed with standard $sudo apt-get install postgresql postgresql-contrib libpq-dev )
is more strict and requires password auth so when you run
rails db:create
you will get error
ActiveRecord::ConnectionNotEstablished: connection to server at "127.0.0.1", port 5432 failed: fe_sendauth: no password supplied (ActiveRecord::ConnectionNotEstablished)
in order to get around this you need to edit file
sudo vim /etc/postgresql/14/main/pg_hba.conf
and have values like this
# Database administrative login by Unix domain socket local all all trust # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all ::1/128 trust
After save
sudo service postgresql restart
credit https://gist.github.com/p1nox/4953113
Now if you run
rails db:create
you get new error
PG::ConnectionBad: connection to server at "127.0.0.1", port 5432 failed: FATAL: role "thisisyou" does not exist (PG::ConnectionBad)
thisisyou is your Ubuntu user, without specifying an user in database.yml Ubuntu will use system user name as postgres user = we need to create postgres user
sudo -u postgres psql
then
CREATE USER thisisyou WITH PASSWORD ''; ALTER USER thisisyou WITH SUPERUSER'