This script is designed to manage PostgreSQL databases. It provides options to create a new database, delete an existing database, and display help information. The script ensures proper user inputs and handles errors effectively.
- PostgreSQL: Ensure PostgreSQL is installed and properly configured on your system.
- Privileges: The script requires superuser privileges to execute database commands. It must be run with
sudo
.
- Create a New Database: Prompts for database name, user, and password, then creates the database and user, and grants all privileges to the user.
- Delete an Existing Database: Lists all non-template databases, allows selection of a database to delete, and confirms the deletion.
- Help: Displays usage information.
To run the script, use the following command:
sudo ./postgres_db_tool.sh
Upon running the script, you will be presented with a menu:
- Create a New Database:
- Prompts for the database name, user, and password.
- Creates the database, user, and grants privileges.
- Logs operations to
/tmp/db_script.log
.
- Delete an Existing Database:
- Lists all available databases.
- Prompts to select a database to delete.
- Confirms the deletion.
- Logs operations to
/tmp/db_script.log
.
- Help:
- Displays usage information and descriptions of the script's functionality.
-
Create a New Database:
Enter the database name: example_db Enter the database user: example_user Enter the database password: ********
-
Delete an Existing Database:
Fetching available databases...
Available Databases:
1. example_db 2. test_db Enter the number of the database to delete: 1 Are you sure you want to delete the database 'example_db'? (yes/no): yes`
- The script logs messages and errors to
/tmp/db_script.log
.
- display_separator: Displays a separator line.
- log_message: Logs messages to the console and the log file.
- create_database: Handles database creation, user creation, and privilege granting.
- delete_database: Lists databases, allows selection and deletion of a database.
- show_help: Displays help information.
- check_permissions: Ensures the script is run with superuser privileges.
- Errors are captured and logged to specific files:
- Database creation errors:
/tmp/db_create_error.log
- User creation errors:
/tmp/db_user_error.log
- Privilege granting errors:
/tmp/db_grant_error.log
- Database deletion errors:
/tmp/db_delete_error.log
- Database creation errors:
For any issues or contributions, please refer to the repository or contact kibe.