Run Postgresql inside Docker container for local development

Run Postgresql inside container with persistent data in docker volume. Useful for local development.

docker run --name my_postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5433:5432 -d -v pgdata:/var/lib/postgresql/data postgres:14.5
  • –name Name you container
  • -e POSTGRES_PASSWORD=mysecretpassword Option for postgres user password
  • -p 5433:5432 Option to expose port, 5433 will be exposed to the local machine
  • -d Option for detached mode, running in background
  • -v pgdata:/var/lib/postgresql/data Option to create pgdata volume and connect it inside container folder /var/lib/postgresql/data
  • postgres:14.5 Postgres image and version. Find more here.

If you do not believe me that the data will be saved even when container is deleted, try this:

##Connect to database
psql -h localhost -p 5433 -U postgres -d postgres

##Create a database
CREATE DATABASE dvdrental;

### import testing database. Download and extract this one for example: http://blog.twis7.com/dvdrental.zip
pg_restore -h localhost -p 5433 -U postgres -d dvdrental /path/to/downloaded/dabase/dvdrental.tar

## connect to database
\c dvdrental

## display all tables
\dt

## stop and remove the container
docker stop my_postgres
docker rm my_postgres

## start the container again
docker run --name my_postgres -e POSTGRES_PASSWORD=mysecretpassword -p 5433:5432 -d -v pgdata:/var/lib/postgresql/data postgres:14.5

##Check the database again. Everything will be inside as expected.

Convert file Encoding in the Linux Terminal (e.g., for Plex Subtitles)

If you’re using Plex and run into subtitle issues, it might be due to incorrect text encoding. Plex only supports UTF-8 encoded subtitle files. Luckily, converting file encodings in Linux is simple using iconv, a tool that comes preinstalled on most modern distributions.

Example: Convert from Windows-1251 to UTF-8

iconv -f WINDOWS-1251 -t UTF-8 sourcefile.srt > newfile.srt

Flags explained:

  • --from-code, -f: Specifies the encoding of the source file.
  • --to-code, -t: Specifies the desired encoding for the output file.

How to Detect a file’s Encoding

Before converting, you can check the current encoding of a file using uchardet:

uchardet sourcefile.srt

This tool will output the most likely character encoding of the file, which you can then use with iconv.

Create new Linux user on Ubuntu server with SSH public access key only

Add the user

sudo useradd -s /bin/bash -d /home/userubuntu/ -m -G sudo userubuntu

Create authorized_keys file and put you public ssh key

mkdir /home/userubuntu/.ssh/
chmod 0700 /home/userubuntu/.ssh/

sudo -- sh -c "echo 'put the ssh-rsa public key here' > /home/userubuntu/.ssh/authorized_keys"
sudo chown -R userubuntu:userubuntu /home/userubuntu/.ssh/

Allow the user to use sudo without password, type sudo visudo and edit the file, adding userubuntu ALL=(ALL) NOPASSWD:ALL like this

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
dimitar ALL=(ALL) NOPASSWD:ALL

Allow only public key access, no password allowed. sudo vim /etc/ssh/sshd_config

ChallengeResponseAuthentication no
PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
UsePAM no

restart the ssh service : sudo systemctl restart sshd

Site to Site ipsec VPN with Strongswan and AWS

The purpose of this article is to explain how to create site to site VPN from a Linux machine to the AWS cloud. I will use Strongswan installed on Ubuntu 20.04 in the Scaleway cloud and AWS VPC. Scaleway offers (at least for now) VPS with Private Subnet and Public IP connected via NAT. In the AWS cloud, I already created VPC with one public and one private subnet – If you do not know how to do it, Neal Davis explains in this video.
In the end, we will have ipsec tunnel, connecting our Linux server with the resources of AWS, placed in a private subnet.

Continue reading “Site to Site ipsec VPN with Strongswan and AWS” →