From a8ef9f87262110ab7119d799aa2d370544e63a7a Mon Sep 17 00:00:00 2001 From: Richie Zhang <12566991+StaticallyTypedRice@users.noreply.github.com> Date: Wed, 5 Feb 2020 18:15:28 -0800 Subject: [PATCH 1/4] Create db-init.sh --- server/db-init.sh | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 server/db-init.sh diff --git a/server/db-init.sh b/server/db-init.sh new file mode 100644 index 000000000..77b9a6d71 --- /dev/null +++ b/server/db-init.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +username=lemmy +dbname=lemmy +port=5432 + +read -p "Enter database password: " -s password +echo + +psql -c "CREATE USER $username WITH PASSWORD '$password' SUPERUSER;" -U postgres +psql -c 'CREATE DATABASE $dbname WITH OWNER $username;' -U postgres +export LEMMY_DATABASE_URL=postgres://$username:$password@localhost:$port/$dbname From 7612a548940fe1a1a69ba6e7cf2197cc407020b4 Mon Sep 17 00:00:00 2001 From: Richie Zhang <12566991+StaticallyTypedRice@users.noreply.github.com> Date: Thu, 6 Feb 2020 12:25:13 -0800 Subject: [PATCH 2/4] Add instructions for unning db-init.sh to contributing_local_development.md --- docs/src/contributing_local_development.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/src/contributing_local_development.md b/docs/src/contributing_local_development.md index c19bcba84..7d782925a 100644 --- a/docs/src/contributing_local_development.md +++ b/docs/src/contributing_local_development.md @@ -7,9 +7,18 @@ #### Set up Postgres DB ```bash - psql -c "create user lemmy with password 'password' superuser;" -U postgres - psql -c 'create database lemmy with owner lemmy;' -U postgres - export LEMMY_DATABASE_URL=postgres://lemmy:password@localhost:5432/lemmy +# Start at the root of the Lemmy repository + +cd server +./db-init.sh +``` + +Or run the commands manually: + +```bash +psql -c "create user lemmy with password 'password' superuser;" -U postgres +psql -c 'create database lemmy with owner lemmy;' -U postgres +export LEMMY_DATABASE_URL=postgres://lemmy:password@localhost:5432/lemmy ``` #### Running From 510387874112f4bf91df7357b731cf0e055f1761 Mon Sep 17 00:00:00 2001 From: Richie Zhang <12566991+StaticallyTypedRice@users.noreply.github.com> Date: Thu, 6 Feb 2020 12:26:01 -0800 Subject: [PATCH 3/4] Add instructions for unning db-init.sh to administration_configuration.md --- docs/src/administration_configuration.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/src/administration_configuration.md b/docs/src/administration_configuration.md index 73ea35042..c47d32362 100644 --- a/docs/src/administration_configuration.md +++ b/docs/src/administration_configuration.md @@ -4,3 +4,12 @@ Additionally, you can override any config files with environment variables. Thes `LEMMY__DATABASE__POOL_SIZE=10`. An additional option `LEMMY_DATABASE_URL` is available, which can be used with a PostgreSQL connection string like `postgres://lemmy:password@lemmy_db:5432/lemmy`, passing all connection details at once. + +If the Docker container is not used, manually create the database specified above by running the following commands: + +```bash +# Start at the root of the Lemmy repository + +cd server +./db-init.sh +``` From 1d4dc19d6f2af133d8a4b6bd77a33cdb8d73aebc Mon Sep 17 00:00:00 2001 From: Richie Zhang <12566991+StaticallyTypedRice@users.noreply.github.com> Date: Thu, 6 Feb 2020 13:07:34 -0800 Subject: [PATCH 4/4] Implement password verification in db-init.sh. --- server/db-init.sh | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/server/db-init.sh b/server/db-init.sh index 77b9a6d71..c9150e9de 100644 --- a/server/db-init.sh +++ b/server/db-init.sh @@ -4,9 +4,40 @@ username=lemmy dbname=lemmy port=5432 -read -p "Enter database password: " -s password -echo +password="" +password_confirm="" +password_valid=0 + +while [ "$password_valid" == 0 ] +do + read -p "Enter database password: " -s password + echo + + read -p "Verify database password: " -s password_confirm + echo + echo + + # Start the loop from the top if either check fails + if [ -z "$password" ] + then + echo "Error: Password cannot be empty." 1>&2 + echo + continue + fi + if [ "$password" != "$password_confirm" ] + then + echo "Error: Passwords don't match." 1>&2 + echo + continue + fi + + # Set the password_valid variable to break out of the loop + password_valid=1 +done + psql -c "CREATE USER $username WITH PASSWORD '$password' SUPERUSER;" -U postgres psql -c 'CREATE DATABASE $dbname WITH OWNER $username;' -U postgres export LEMMY_DATABASE_URL=postgres://$username:$password@localhost:$port/$dbname + +echo $LEMMY_DATABASE_URL