Install and configure Postgres
-
Install Postgres:
sudo apt install postgresql
-
Start the postgres console as
postgres
user so you can add a user with your linux' user name:sudo -u postgres psql
-
Create a user with the same username as your linux user and the right to create databases:
postgres=# CREATE USER judith WITH createdb superuser;
More info in the postgres docs Show archive.org snapshot -
Check that the command succeeded by listing the users known to postgres:
\du
All together:
~ >sudo -u postgres psql psql (12.6 (Ubuntu 12.6-0ubuntu0.20.04.1)) Type "help" for help. postgres=# CREATE USER judith WITH createdb superuser; CREATE ROLE postgres=# \du List of roles Role name | Attributes | Member of -----------+------------------------------------------------------------+----------- judith | Superuser, Create DB | {} postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
-
Check
/etc/postgresql/12/main/postgresql.conf
. Ensure that listen is restricted to localhost:#listen_addresses = 'localhost' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost'; use '*' for all
-
Edit
pg_hba.conf
to trust all local connections. This will fixfe_sendauth: no password supplied
errors.
Open file/etc/postgresql/12/main/pg_hba.conf
and change to following lines totrust
:# Database administrative login by Unix domain socket local all postgres 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
-
Restart postgres so that the changes are applied:
sudo systemctl restart postgresql
Configure your Rails app
-
Add
gem 'pg'
to yourGemfile
and bundle -
If you encounter problems with
libpq-fe.h
when bundling, installlibpq-dev
:sudo install libpq-dev
-
Change your projects
config/database.yml
to use postgres:default: &default adapter: postgresql encoding: unicode host: localhost username: password: development: <<: *default database: zitate_dev test: <<: *default database: zitate_test
Note that you do not have to include a username, password and port (at least as long as postgres runs on its default port 5432)