I use Ubuntu for developing Ruby on Rails apps, and usually use PostgreSQL for the database.
On Ubuntu 22 this is how I install and setup PostgreSQL on a newly installed machine.
-
Install PostgreSQL on the machine, and the
psqlclient:sudo apt-get install postgresql-14 postgresql-client -
Open the PostgreSQL shell as the
postgresuser:sudo -u postgres psql -
In the
psqlshell, create or alter a user with the rights to create databases. Make sure the username matches your Ubuntu username:create user viktor with superuser;OR if the
psqlalready user exists:alter user viktor with superuser;
You can verify if your use has superuser privileges with this command:
\du
And then exit the psql shell by typing exit or CTRL+D
With these settings there should be no need to edit any PostgreSQL config files.
In a new Ruby on Rails project, you should be able to create databases for example using:
rails db:setup
The solution above should prevent errors like:
PG:ConnectionBad:connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: No such file or directory. Is the server running locally and accepting connections on that socket?ActiveRecord::NoDatabaseError:connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: FATAL: role does not existActiveRecord::NoDatabaseError:connection to server on socket “/var/run/postgresql/.s.PGSQL.5432” failed: FATAL: database does not exist