Gerson Barreiros
Linux Systems, Networking, Firewalls, DevOps & Kubernetes
MariaDB & MySQL:
sudo apt install default-mysql-client # or: mariadb-client
mysqlcheck -h yourdatabasehost -u root -p --auto-repair --check --all-databases mysqlcheck -h yourdatabasehost -u root -p --auto-repair --optimize --all-databases # Note: MariaDB 11+ also provides mariadb-check; --optimize rebuilds InnoDB tables, so schedule on busy databases carefully.
PostgreSQL:
vacuumdb -afvz -h yourdatabasehost -U postgres
reindexdb -a -h yourdatabasehost -U postgres
Updated (November 4, 2021): Docker example:
docker exec -it postgres-server vacuumdb -afvz -U postgres