facilitating postgres database migration
This commit is contained in:
parent
150de58a2e
commit
6181bb2711
|
@ -1 +1 @@
|
|||
{}
|
||||
{"taxonomy":{"tags":["databases","devops","postgres","redis","servers"]}}
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"[markdown]": {
|
||||
}
|
||||
}
|
|
@ -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"
|
||||
]
|
||||
}
|
|
@ -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 <host> -p <port> -U <username> -d <database> > <outputfile>
|
||||
```
|
||||
|
||||
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 <host> -p <port> -U <username> -d <database> < <inputfile>
|
||||
```
|
||||
|
||||
You can also pretty easily combine the two commands using a pipe:
|
||||
|
||||
```bash
|
||||
pg_dump -h <host> -p <port> -U <username> -d <database> | psql -h <host> -p <port> -U <username> -d <database>
|
||||
```
|
||||
|
||||
## 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 <source> <destination>
|
||||
# Example: pgmigrate postgres://user:pass@localhost:5432/source postgres://user:pass@localhost:5432/destination
|
||||
|
||||
set -e
|
||||
|
||||
if [ $# -ne 2 ]; then
|
||||
echo "Usage: pgmigrate <source> <destination>"
|
||||
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.
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue