setup/doc/postgres.md

117 lines
3.1 KiB
Markdown
Raw Normal View History

2022-04-14 10:21:05 +02:00
# Setup Postgres
* get the latest version of installed packages and refresh the repo cache
```
sudo apt update
```
* install postgres directly from the official repository
```
2022-04-22 15:43:30 +02:00
sudo apt-get install postgresql --no-install-recommends
2022-04-14 10:21:05 +02:00
```
2022-04-22 15:43:30 +02:00
* validate the existence of the postgres admin user
2022-04-14 10:21:05 +02:00
```
cat /etc/passwd|grep postgres
cat /etc/group|grep postgres
```
* set key for user postgres
```
2022-05-03 11:29:34 +02:00
sudo -u postgres psql
2022-04-14 10:21:05 +02:00
postgres=# \password postgres
```
2022-05-03 11:29:34 +02:00
* create a new database with createdb command, which is going to be owned by user <user name>
2022-04-14 10:21:05 +02:00
```
2022-05-03 11:29:34 +02:00
sudo -u postgres psql
CREATE DATABASE <database name>;
2022-04-14 10:21:05 +02:00
```
2022-05-03 11:29:34 +02:00
* create a new database user
2022-04-14 10:21:05 +02:00
```
2022-05-03 11:29:34 +02:00
sudo -u postgres psql
CREATE USER <user name> with encrypted password <key>;
grant all privileges on database <database name> to <user name>;
2022-04-14 10:21:05 +02:00
```
2022-04-22 15:43:30 +02:00
* edit the pg_hba.conf file\
NOTE: Insert the postgresql database version that is running on the respective host
2022-04-14 10:21:05 +02:00
```
2022-04-22 15:43:30 +02:00
sudo cp /etc/postgresql/<psql version>/main/pg_hba.conf /etc/postgresql/<psgl version>/main/pg_hba.conf-backup
sudo vi /etc/postgresql/<psql version>/main/pg_hba.conf
2022-04-14 10:21:05 +02:00
```
2022-05-03 11:29:34 +02:00
* in order to be able to run applications like e.g. Spring Boot with a local PostgreSQL installation, change the authentication method for the Unix domain socket and local connections to trust like this
2022-04-14 10:21:05 +02:00
```
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
```
* restart PostgreSQL to enable the changes
```
sudo systemctl restart postgresql
systemctl status postgresql
```
* use the psql tool to connect to the database
```
psql -U <user name> -d <database name> -W
```
* configure ports and update firewall using the [firewall setup](firewall.md)
```
sudo ufw allow 5432
sudo ufw enable
sudo ufw status numbered
```
# Allow Remote Connections
* create config backup
```
2022-04-22 15:43:30 +02:00
sudo cp /etc/postgresql/<version>/main/postgresql.conf /etc/postgresql/<version>/main/postgresql.conf-backup
2022-04-14 10:21:05 +02:00
```
2022-04-28 14:05:52 +02:00
* open config file to define what IP addresses postgres to listen on
2022-04-14 10:21:05 +02:00
```
2022-04-22 15:43:30 +02:00
sudo vi /etc/postgresql/<version>/main/postgresql.conf
2022-04-14 10:21:05 +02:00
```
* edit config file like this
```
#listen_addresses = 'localhost'
listen_addresses = '*'
```
2022-04-28 14:05:52 +02:00
* open config file to define access to all databases for all users with an encrypted key
2022-04-14 10:21:05 +02:00
```
2022-04-22 15:43:30 +02:00
sudo vi /etc/postgresql/<version>/main/pg_hba.conf
2022-04-14 10:21:05 +02:00
```
* edit config file like this
```
# TYPE DATABASE USER ADDRESS METHOD
host all all 0.0.0.0/0 md5
host all all :/0 md5
```
* restart PostgreSQL to enable the changes
```
sudo systemctl restart postgresql
systemctl status postgresql
```
# Disable Start Up At System Boot
* If you install the PostgreSQL database from packages, it is automatically added to the start up scripts of the operating system. If you are only learning to work with the database, it is unnecessary to start the database each time you boot the system. Remove system startup link for the PostgreSQL database
```
sudo update-rc.d -f postgresql remove
```
# Links
* [PostgreSQL Java](https://zetcode.com/java/postgresql/)