From 3ab07df114b687a51148d6481697d773dadb4ffd Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Thu, 16 Nov 2023 10:23:12 +0100 Subject: [PATCH] feat: add doc/postgres-upgrade-pg_upgrade.md and doc/postgres-upgrade-pg_upgradecluster.md --- doc/postgres-connect.md | 19 ++---- doc/postgres-create-db.md | 6 +- doc/postgres-create-user.md | 11 ++- doc/postgres-upgrade-pg_upgrade.md | 65 ++++++++++++++++++ doc/postgres-upgrade-pg_upgradecluster.md | 81 +++++++++++++++++++++++ 5 files changed, 158 insertions(+), 24 deletions(-) create mode 100644 doc/postgres-upgrade-pg_upgrade.md create mode 100644 doc/postgres-upgrade-pg_upgradecluster.md diff --git a/doc/postgres-connect.md b/doc/postgres-connect.md index 16dd0fd..bc1b22a 100644 --- a/doc/postgres-connect.md +++ b/doc/postgres-connect.md @@ -1,28 +1,17 @@ # Connect to Postgres Database Using Psql -* configure Postgres like [this](./postgres.md) - -* create a database like [this](./postgres-create-db.md) - -* create a use like [this](./postgres-create-user.md) - -* when needed connect to Postgres as admin user locally +* OPTION: connect to Postgres as admin user ``` -sudo -u postgres psql postgres -W +sudo -u postgres psql -U postgres -W ``` -* when needed connect to the database -as user locally +* OPTION: connect to the database as user ``` psql -U -d -W ``` -* when needed connect to the database -as user to a remote Postgres instance at host - and port \ -NOTE: Configure Postgres for remote connections like -[this](./postgres.md) +* OPTION: connect to the database as user to a remote Postgres instance at host and port (NOTE: Configure Postgres for remote connections like [this](./postgres.md)) ``` psql -h -p -U -d -W ``` \ No newline at end of file diff --git a/doc/postgres-create-db.md b/doc/postgres-create-db.md index b1b5781..57467c6 100644 --- a/doc/postgres-create-db.md +++ b/doc/postgres-create-db.md @@ -1,9 +1,9 @@ # Create Database with Postgres -* configure Postgres like [this](./postgres.md) - -* create a new database +* create a new database and grant privileges to ``` sudo -u postgres psql -W CREATE DATABASE ; +GRANT ALL PRIVILEGES ON DATABASE to ; +\q ``` diff --git a/doc/postgres-create-user.md b/doc/postgres-create-user.md index 6683196..e3fe87f 100644 --- a/doc/postgres-create-user.md +++ b/doc/postgres-create-user.md @@ -1,13 +1,12 @@ # Create User with Postgres -* configure Postgres like [this](./postgres.md) - -* create a database like [this](./postgres-create-db.md) - * create a database user called with password ``` -sudo -u postgres psql -W CREATE USER with encrypted password ''; +``` + +* OPTION: gant user access to database +``` GRANT ALL PRIVILEGES ON DATABASE TO ; ``` @@ -24,7 +23,7 @@ ALTER USER WITH ENCRYPTED PASSWORD ''; * OPTION: gant user access to database ```postgres``` so that this user is able to login without providing a database ``` sudo -u postgres psql -W -grant all privileges on database postgres to ; +GRANT ALL PRIVILEGES ON DATABASE postgres to ; ``` * OPTION: gant user permission to create databases diff --git a/doc/postgres-upgrade-pg_upgrade.md b/doc/postgres-upgrade-pg_upgrade.md new file mode 100644 index 0000000..4b8df6e --- /dev/null +++ b/doc/postgres-upgrade-pg_upgrade.md @@ -0,0 +1,65 @@ +# Upgrade using pg_upgrade + +* check clusters +``` +pg_lsclusters +``` + +* do +``` +sudo apt update +sudo apt upgrade postgresql-xy +``` + +* install relevant packages +``` +sudo apt-get install postgresql-server-dev-13 +``` + +* make a backup +``` +cd +time sudo -u postgres pg_dumpall > bckp-all-2023-08-07 +``` + +* check installed extensions on old version +``` +sudo -u postgres psql -p 5432 postgres +\dx +``` + +* install extension on new version +``` +sudo apt install postgresql-13-postgis-3 --no-install-recommends +sudo -u postgres psql -p 5433 postgres +\dx +CREATE EXTENSION postgis; +CREATE EXTENSION hstore; +``` + +* stop all clusters +``` +sudo systemctl stop postgresql +systemctl status postgresql +pg_lsclusters +``` + +* migrate +``` +sudo -iu postgres /usr/lib/postgresql/13/bin/pg_upgrade \ +-o "-c config_file=/etc/postgresql/11/main/postgresql.conf" \ +--old-datadir=/var/lib/postgresql/11/main/ \ +-O "-c config_file=/etc/postgresql/13/main/postgresql.conf" \ +--new-datadir=/var/lib/postgresql/13/main/ \ +--old-bindir=/usr/lib/postgresql/11/bin \ +--new-bindir=/usr/lib/postgresql/13/bin --check --jobs=4 +``` + +* TODO tbc + +* uninstall the previous version +``` +sudo apt purge postgresql-11* postgresql-client- 11* +sudo apt autoremove +apt list --installed | grep postgresql-11 postgresql-client-11* +``` diff --git a/doc/postgres-upgrade-pg_upgradecluster.md b/doc/postgres-upgrade-pg_upgradecluster.md new file mode 100644 index 0000000..42bfcda --- /dev/null +++ b/doc/postgres-upgrade-pg_upgradecluster.md @@ -0,0 +1,81 @@ +# Upgrade using pg_upgradecluster + +* check clusters +``` +pg_lsclusters +``` + +* do +``` +sudo apt update +sudo apt upgrade postgresql-xy +``` + +* install relevant packages +``` +sudo apt-get install postgresql-server-dev-13 +``` + +* make a backup +``` +cd +time sudo -u postgres pg_dumpall > bckp-all-2023-08-07 +``` + +* check installed extensions on old version +``` +sudo -u postgres psql -p 5432 postgres +\dx +``` + +* OPTION: install extension on new version +``` +sudo apt install postgresql-13-postgis-3 --no-install-recommends +sudo -u postgres psql -p 5433 postgres +\dx +CREATE EXTENSION postgis; +CREATE EXTENSION hstore; +``` + +* stop all clusters +``` +sudo systemctl stop postgresql +systemctl status postgresql +pg_lsclusters +``` + +* migrate +``` +sudo systemctl stop postgresql@13-main +sudo -u postgres pg_dropcluster 13 main +sudo -u postgres pg_upgradecluster 11 main +sudo systemctl daemon-reload +systemctl status postgresql@13-main +pg_lsclusters +``` + +* drop the old cluster: +``` +sudo -u postgres pg_dropcluster 11 main +``` + +* uninstall the previous version +``` +sudo apt purge postgresql-11* postgresql-client-11* +sudo apt autoremove +apt list --installed | grep postgresql-11 postgresql-client-11* +``` + +* do +``` +sudo pg_ctlcluster 13 main stop +pg_lsclusters +sudo pg_ctlcluster 13 main start +pg_lsclusters +``` + +* vacuum +``` +sudo -u postgres vacuumdb --all --analyze-only +sudo -u postgres vacuumdb --all +```