Skip to main content

Posts

Showing posts with the label database

Backup and Restore PostgreSQL Database

PostgreSQL has been shipped with a lot of tools for ease of managing the database. Some actions that we typically perform in managing databases are backing up a database and restoring it to another database server. In PostgreSQL, we can utilize pg_dump to back up the data from a database and psql or pg_restore to restore the data into a new database. Generating A Backup File There are several available formats for the exported data: plain, directory, compressed, and custom. The plain format is using plain SQL syntax to export the data. To make the tables are exported separately, we can utilize directory format. The custom format is utilizing a built-in compression mechanism and results in an optimized binary formatted file. It is suitable for a large database. If we use a custom format, we can only restore it using pg_restore , but we have the ability to selectively choose desired tables to be restored. The following command is used to generate a plain formatted file with th...

Creating Connection in TypeORM

TypeORM is a complete ORM library for Node.js and other Javascript-based platforms which has support for a variety of databases such as PostgreSQL, MySQL, MongoDB, and so on. It can be utilized using Typescript or Javascript. There are several methods that can be used for creating connections in TypeORM. Create single connection await createConnection({ name, type, host, port, username, password, database }); Then, we can take the connection everywhere using getConnection() . const conn: Conection = getConnection(); Not only the connection, but we can also directly utilize EntityManager and Repository of the connection everywhere. const users = getManager().find(User); const user = getRepository(User).findOne(id); Another method is using getConnectionManager() . const connManager = getConnectionManager(); const conn = connManager.create({ ...params }); await conn.connect(); Create multiple connections await createConnections([{ ...params1 }, { ...params2 }]); Then, we ...

Securing Redis to Be Accessed From All Interfaces

Redis can bind to all interfaces with bind * -::* configuration. But, Redis also enables protected-mode by default in its configuration file. It will make bind * -::* configuration ineffective because the protected-mode requires both to explicitly state the binding interfaces and to define user authentication. The unsecured way is to set protected-mode no in the configuration. It will make our Redis server becomes accessible from any interfaces without authentication. It may be fine if we deploy our Redis server in a closed environment such as in a containerized one without exposing and pointing any port to the Redis service port. So that, the service can only be accessible from other services in the container's network. The recommended way is to keep protected-mode yes in the configuration. Then, we need to add a new user authentication configuration and limiting access for the default user. A default user is a user with no assigned name when the client tries to connect ...

Persisting Data and Replication in Redis

As we know, Redis is an in-memory key-value store database. If our data is stored in our host memory (RAM), how can we restore all values from the last state of our system in case of system reboot or power outages? Redis provides two options for persisting our data. The first is by creating a snapshot and the second is by appending each write action into a file. The second is also called the append-only-file (AOF) method. Applying those options is as trivial as updating several lines of the Redis configuration file. Redis performs snapshotting with certain rules by default. Enabling the auto-snapshot method with different rules is done by configuring the following lines in the /etc/redis/redis.conf file. save 300 10 save 30 1000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir /var/lib/redis The line save 300 10 means snapshot will be automatically updated in the background if at least 10 writes have occurred within 300 seconds. The li...

Setup MongDB on Windows

Download and install MongoDB . You can set custom installation directory or let default location in "C:\Program Files\MongoDB\Server\<version>". Add "C:\Program Files\MongoDB\Server\<version>\bin" to environment variable. Create directory for database data and log data. For example, database is in "C:\mongodb\data\db\" and log is in "C:\mongodb\log\". Create MongoDB configuration file. For example, it is in "C:\mongodb\mongod.conf". systemLog: destination: file path: c:\mongodb\log\mongod.log logAppend: true storage: dbPath: c:\mongodb\data\db (CAUTION: Use spaces, not tabs) Start the server by: $ mongod --config "C:\mongodb\mongod.conf" OR install it as a Windows service $ mongod --config "C:\mongodb\mongod.conf" --install Then, start the service. $ net start MongoDB To stop the service. $ net stop MongoDB To remove the service after stoping the service. ...