|
This page will tell you the short and to the point stuff you need to know about using Postgres DB's
|
|
This page will tell you the short and to the point stuff you need to know about using Postgres DB's
|
|
|
|
|
|
- [1. Install on Ubuntu 20.\*](#1-install-on-ubuntu-20)
|
|
- [1. Install](#1-install)
|
|
- [2. Passwords](#2-passwords)
|
|
- [2. Passwords](#2-passwords)
|
|
- [3. Interact with DB](#3-interact-with-db)
|
|
- [3. Interact with DB](#3-interact-with-db)
|
|
|
|
- [3.1. With DBeaver](#31-with-dbeaver)
|
|
|
|
- [3.2. From VPS/Container terminal](#32-from-vpscontainer-terminal)
|
|
- [4. Backup restore](#4-backup-restore)
|
|
- [4. Backup restore](#4-backup-restore)
|
|
- [4.1. Daily backup](#41-daily-backup)
|
|
- [4.1. Daily backup](#41-daily-backup)
|
|
- [4.2. Restore](#42-restore)
|
|
- [4.2. Restore](#42-restore)
|
|
|
|
|
|
|
|
|
|
# 1. Install on Ubuntu 20.*
|
|
# 1. Install
|
|
|
|
|
|
```bash
|
|
Easiest is using docker for that:
|
|
sudo apt update
|
|
|
|
sudo apt install postgresql postgresql-contrib
|
|
|
|
sudo systemctl start postgresql.service
|
|
|
|
```
|
|
|
|
|
|
|
|
Check status: `sudo systemctl status postgresql.service`
|
|
1. Install docker on your local machine: E.g. on [Windows](https://docs.docker.com/desktop/install/windows-install/) or on [Linux Ubuntu](https://docs.docker.com/engine/install/ubuntu/).
|
|
|
|
2. To start execute command: `docker run -p 5432:5432 --name wyatt_local_db -e POSTGRES_USER=wyatt -e POSTGRES_PASSWORD=somepasswd -e POSTGRES_DB=wyatt -d postgres:15.4`
|
|
|
|
3. To restart: `docker start wyatt_local_db` (on Windows this can be done in app **Docker Desktop**).
|
|
|
|
|
|
See [this tutorial](https://www.digitalocean.com/community/tutorials/how-to-install-postgresql-on-ubuntu-20-04-quickstart) for more info.
|
|
|
|
|
|
|
|
# 2. Passwords
|
|
# 2. Passwords
|
|
|
|
|
|
There are many ways to use a password. For using DB commands from the command line (e.g. from scripts) the preferred way is to add credentials file `~/.pgpass` and limit rights to it. This is how you can do that on a container (which for us is the most common approach). This is the script for a container called `wyatt_local_db` where the DB runs on `localhost:5432`, with a DB and user called `wyatt`:
|
|
There are many ways to use a password. For using DB commands from the command line (e.g. from scripts) the preferred way is to add credentials file `~/.pgpass`. This is how you can do that on a container (which for us is the most common approach). This is the script for a container called `wyatt_local_db` where the DB runs on `localhost:5432`, with a DB and user called `wyatt`:
|
|
|
|
|
|
```bash
|
|
```bash
|
|
docker exec -t wyatt_local_db bash -c 'echo "localhost:5432:wyatt:wyatt:somepasswd" > ~/.pgpass && chmod 0600 ~/.pgpass'
|
|
docker exec -t wyatt_local_db bash -c 'echo "localhost:5432:wyatt:wyatt:somepasswd" > ~/.pgpass && chmod 0600 ~/.pgpass'
|
... | @@ -30,7 +29,56 @@ docker exec -t wyatt_local_db bash -c 'echo "localhost:5432:wyatt:wyatt:somepass |
... | @@ -30,7 +29,56 @@ docker exec -t wyatt_local_db bash -c 'echo "localhost:5432:wyatt:wyatt:somepass |
|
|
|
|
|
# 3. Interact with DB
|
|
# 3. Interact with DB
|
|
|
|
|
|
most often we do that from inside the container
|
|
Both these approaches rely on being able to access the server the database is on. The easiest way to do that is described in [Develop and test on remote server](Develop-and-test-on-a-remote-server). So set that up first.
|
|
|
|
|
|
|
|
## 3.1. With DBeaver
|
|
|
|
|
|
|
|
All work pretty much the same:
|
|
|
|
|
|
|
|
1. Open DBeaver
|
|
|
|
2. CTRL+SHIFT+N (new connection)
|
|
|
|
3. Enter these settings (depending on the type of your connection)
|
|
|
|
|
|
|
|
**Local database **
|
|
|
|
|
|
|
|
Tab | Field | Value
|
|
|
|
--- | ----- | -----
|
|
|
|
Main | Host | `localhost`
|
|
|
|
Main | Database | name of database e.g. `wyatt`
|
|
|
|
Main | Port | port of database e.g. `5432` or `25432`
|
|
|
|
Main | Username | main user of this database e.g. `wyatt`
|
|
|
|
Main | Password | password of main user
|
|
|
|
|
|
|
|
**Running in a container on a VPS**
|
|
|
|
|
|
|
|
Same as local database but also
|
|
|
|
|
|
|
|
Tab | Field | Value
|
|
|
|
--- | ----- | -----
|
|
|
|
SSH | Use SSH tunnel | `checked`
|
|
|
|
SSH | Host/IP | the ip address of the VPS e.g. `31.220.89.50`
|
|
|
|
SSH | Username | main user of this VPS, often `devops`
|
|
|
|
SSH | Authentication method | `Public key`
|
|
|
|
SSH | Authentication method | path to your private key file (the public key should be added on that VPS on a new line in file `/home/devops/.ssh/authorized_keys`)
|
|
|
|
SSH | Passphrase | the passphrase of the private key file (only needed if that file has a passphrase)
|
|
|
|
|
|
|
|
**Running in a Kubernetes cluster on a VPS**
|
|
|
|
|
|
|
|
Same as local database, but you should first have DB port forwarding enabled like this:
|
|
|
|
|
|
|
|
1. Prerequisites:
|
|
|
|
1. If not done yet, install Kubernetes CLI [kubectl](https://kubernetes.io/docs/tasks/tools/#kubectl)
|
|
|
|
2. Follow authentication instructions as described under **Kubernetes Authentication** in XLRIT's `.kdbx` file.
|
|
|
|
3. git clone our XLRIT scripts somewhere on your local computer with: `git clone git@gitlab.xlrit.com:xlrit/gears/scripts.git`.
|
|
|
|
4. Add this scripts folder to you PATH environment variable
|
|
|
|
2. Forward DB connection to your local computer:
|
|
|
|
1. Execute command: `db-forward <Kubernetes namespace of the app e.g. wyatt-tst> <portnumber of your choosing e.g. 25432>`
|
|
|
|
|
|
|
|
> Tip: To get possible name spaces enter: `kubectl get namespace`
|
|
|
|
|
|
|
|
## 3.2. From VPS/Container terminal
|
|
|
|
|
|
|
|
You can also do that from a container:
|
|
|
|
|
|
```bash
|
|
```bash
|
|
docker ps # get a listing of all containers on this machine
|
|
docker ps # get a listing of all containers on this machine
|
... | | ... | |