The package will be used to run the bash commands in Node.js npm install dotenv a file. The dotenv is used to manage environment variables and we will use it to pass the database connection parameters. In this section, we will write a Node.js code that will run the above mentioned bash “pg_dump” command to take the PostgreSQL database backup and then upload the backup file to another server.įirst of all, install the required dependencies using the npm, which is a package manager for Node.js. We will run this command using Node.js code and then upload the backup file to another server. The above command will generate a file db_backup.tar, which will be the backup of the entire PostgreSQL database named pg_database. If you wish to dig deep and explore more options, there is a list of them here.Īdvancing ahead, in order to take backup of the entire database using the pg_dump utility along with the necessary options, use this syntax: pg_dump -U admin -h localhost -p 5432 -f db_backup.tar -F t -d pg_database -d specifies the database name to have a backup of.-f specifies the name of the output file.Other options for database backup include: -p is used to specify the port the database server is listening on.-h is used to specify the hostname for the PostgreSQL server.-U option specifies the database user used to run the command.The following connection options are used with the pg_dump command to take the database backup. NOTE: This post contains commands specific for PostgreSQL 13. Below is the syntax to use the pg_dump command for backups. It is automatically installed with the PostgreSQL installation. The pg_dump is a utility used to take the logical backups of a PostgreSQL database. Then we will write a cron job to schedule this database backup process so it can be performed in a defined window. This database backup will be uploaded on another server using Node.js. These commands will prepare a backup of the PostgreSQL database. In this post, you will learn how to use Node.js to run the bash commands. This way if there is a database server failure, you can always use the latest backup to restore the database. You can schedule automated database backups and save yourself from the hassle of doing them manually. Restore it in production in case of failure. Have a detailed backup of your database.To be sure you can recover your data with minimal data loss: So obviously you should have a disaster recovery plan in place that will cause the least user disruption and data loss. In the event of a hardware or software failure, you risk losing your application’s entire database along with your important data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |