It is quite easy to install the latest version of Postgres on Ubuntu.
First, declare Postgres repository, create the file /etc/apt/sources.list.d/pgdg.list
like this :
1 |
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' |
Import the repository signing key, and update ubuntu package lists
1 2 |
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - sudo apt-get update |
Then install latest postgresql (actually version 11) and its latest GUI pgadmin (actually version 4) :
1 |
sudo apt install postgresql-11 pgadmin4 |
Postgresql is now running, you can check that by pgsql client. Be careful, by default, authentification are based on linux account : only postgres user can connect to the database :
1 2 3 |
$ sudo su - postgres $ psql postgres=# \conninfo |
Create a user, a database and grant privileges :
1 2 3 4 |
sudo -u postgres psql postgres=# create database mydb; postgres=# create user myuser with encrypted password 'mypasswd'; postgres=# grant all privileges on database mydb to myuser; |
The linux account myuser
must exist, so do not forget to create it AND access the database with that user.
If you want to access the database mydb
from any linux account, you have to edit a configuration file :
1 |
sudo vi /etc/postgresql/11/main/pg_hba.conf |
and change this line :
1 |
local all all peer |
To this:
1 |
local all all md5 |
Then restart postgresql :
1 |
sudo service postgresql restart |
You are ready to start your project.
For those who are using python/Django, you will have to install psycopg2 package which requires postgresql dev files :
1 2 |
sudo apt install postgresql-server-dev-11 pip install pycopg2 |
Now you just have to run migrate
to create tables for Django :
1 |
./manage.py migrate |