diff --git a/.frontmatter/database/taxonomyDb.json b/.frontmatter/database/taxonomyDb.json index 9e26dfe..2d676c5 100644 --- a/.frontmatter/database/taxonomyDb.json +++ b/.frontmatter/database/taxonomyDb.json @@ -1 +1 @@ -{} \ No newline at end of file +{"taxonomy":{"tags":["databases","devops","postgres","redis","servers"]}} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f66ad92 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "[markdown]": { + } +} \ No newline at end of file diff --git a/frontmatter.json b/frontmatter.json index 177cbb1..74807be 100644 --- a/frontmatter.json +++ b/frontmatter.json @@ -17,6 +17,12 @@ "name": "description", "type": "string" }, + { + "title": "Tags", + "name": "tags", + "type": "tags", + "single": true + }, { "title": "Publishing date", "name": "date", @@ -25,13 +31,31 @@ "isPublishDate": true }, { - "title": "Content preview", - "name": "heroImage", - "type": "image", - "isPreviewImage": true + "title": "Featured", + "name": "featured", + "type": "boolean" + }, + { + "title": "Draft", + "name": "draft", + "type": "boolean" + }, + { + "title": "OG image", + "name": "ogImage", + "type": "image" + }, + { + "title": "author", + "name": "author", + "type": "string" } ] } ], - "frontMatter.content.supportedFileTypes": [ "md", "markdown", "mdx" ] + "frontMatter.content.supportedFileTypes": [ + "md", + "markdown", + "mdx" + ] } \ No newline at end of file diff --git a/src/content/blog/facilitating-postgres-database-migration.md b/src/content/blog/facilitating-postgres-database-migration.md new file mode 100644 index 0000000..b263e7a --- /dev/null +++ b/src/content/blog/facilitating-postgres-database-migration.md @@ -0,0 +1,88 @@ +--- +title: Facilitating Postgres database migration +description: Postgres databases don't have to be hard to migrate +tags: + - databases + - devops + - servers + - postgres +author: Chris W +date: 2023-10-18T00:01:39.322Z +--- + +On the back of today's other post about [migrating your redis database across servers](/posts/migrating-your-redis-database-to-another-server/) I thought it only made sense to talk about migrating your Postgres database as well, seeing as I migrated my Postgres database for my [Firefish](https://joinfirefish.org) instance as well. + +## Postgres' built-in migration tool + +This probably isn't going to be breaking news to anyone, but Postgres has a built-in tool for migrating databases. It's called `pg_dump` and it's pretty easy to use. The syntax is as follows: + +```bash +pg_dump -h -p -U -d > +``` + +This allows you to pretty easily dump your entire database to a file. This file can then be imported into another database using the `psql` command: + +```bash +psql -h -p -U -d < +``` + +You can also pretty easily combine the two commands using a pipe: + +```bash +pg_dump -h -p -U -d | psql -h -p -U -d +``` + +## Making it even easier + +I'm lazy, so I don't want to have to type out that entire command every time I want to migrate my database. Thankfully, I don't have to. I can just write a simple bash script to do it for me. Here's what I came up with: + +```bash +#! /usr/bin/env bash +# This script is used to migrate a postgres database from one server to another. +# Usage: pgmigrate +# Example: pgmigrate postgres://user:pass@localhost:5432/source postgres://user:pass@localhost:5432/destination + +set -e + +if [ $# -ne 2 ]; then + echo "Usage: pgmigrate " + exit 1 +fi + +SOURCE=$1 +DESTINATION=$2 + +echo "Migrating from $SOURCE to $DESTINATION" + +echo "Dumping source database" +pg_dump $SOURCE > /tmp/dump.sql + +if ! psql $DESTINATION -c "select 1" > /dev/null 2>&1; then + echo "Destination database does not exist. Creating it" + dbname=$(echo $DESTINATION | sed -e 's/.*\///') + dest=$(echo $DESTINATION | sed -e 's/\/[^/]*$//') + psql $dest -c "create database $dbname" +fi + +echo "Restoring to destination database" +psql $DESTINATION < /tmp/dump.sql + +echo "Cleaning up" +rm /tmp/dump.sql + +echo "Done" +``` + +This script takes two arguments, the source database and the destination database. It then dumps the source database to a file, creates the destination database if it doesn't exist, and then restores the source database to the destination database. It then cleans up after itself and exits. + +Here's an example of how to use it: + +```bash +pgmigrate postgres://user:pass@localhost:5432/source postgres://user:pass@localhost:5432/destination +``` + +How simple is that? + +## Conclusion + +I hope this post was helpful to you. I don't see this one being as niche and potentially useful as the redis one, but I figured I'd write it up anyway, if for no othr reason than my propensity for borking my linux system, losing my files, and having to start over from scratch. diff --git a/src/content/blog/migrating-your-redis-database-to-another-server.md b/src/content/blog/migrating-your-redis-database-to-another-server.md index d0c645c..040e4f0 100644 --- a/src/content/blog/migrating-your-redis-database-to-another-server.md +++ b/src/content/blog/migrating-your-redis-database-to-another-server.md @@ -4,9 +4,10 @@ date: 2023-10-17T18:53:50.702Z title: Migrating your Redis database to another server description: How to migrate your Redis database to another server using replication tags: + - databases - redis - servers - - databases + - devops --- Seeing this title you might be thinking to yourself, _why would I ever need to do that?_. After all, redis is meant to be used as a throwaway cache right? Caches by their very nature are generally disposable, so what would posess you to want to migrate that cache somewhere else? Well, I can't speak for you, but I can tell you why I needed to do it.