postgresync
Replace the PostgreSQL data on a remote server with a copy of this server's PostgreSQL data directory (rsync over SSH)
- Command:
/go/postgresync - Category: Migration
- Required options:
<remote-server>,<remote-password> - Output: rsync progress
- Last updated: 2026-11-20
On this page: Overview · Quick start · Syntax · Options · Output · Examples · Troubleshooting · Details · See also
Overview
/go/postgresync copies all of this server's PostgreSQL data, meaning every database, user, and setting, to another server. It is meant for moving PostgreSQL to a new server.
It works on the files of the database, not on the SQL inside. It:
- connects to the remote server as
rootand stops PostgreSQL there - copies the data directory
/var/lib/pgsql/data/withrsync - gives the
postgresuser ownership of the copied files - restarts PostgreSQL on the remote server
[!WARNING]
- Everything in PostgreSQL on the remote server is replaced, including its databases, users, and configuration files.
- PostgreSQL on this server is not stopped. Files that change during the copy can give you a damaged copy. Stop PostgreSQL here before you run the command (see Examples).
Quick start
# Stop PostgreSQL here so the files don't change during the copy
systemctl stop postgresql
# Copy it to the remote server
/go/postgresync 203.0.113.5 'RemoteRootPassword'
# Start PostgreSQL here again
systemctl start postgresql
Syntax
/go/postgresync <remote-server> <remote-password> [port]
Options
| Argument | Required | Default | Notes |
|---|---|---|---|
<remote-server> |
Yes | — | The IP address or host name of the remote server. |
<remote-password> |
Yes | — | The remote server's root password. |
[port] |
No | 22 |
The SSH port of the remote server. |
Both servers need:
- PostgreSQL installed, with the same major version (see
postgresql) - the data directory at
/var/lib/pgsql/data/, which is the layout on AlmaLinux, Rocky, CentOS, Red Hat, and Fedora
The remote server must allow root to log in over SSH with a password (see permitrootlogin) and have rsync. This server needs sshpass and rsync.
Output
Copying PostgreSQL to 203.0.113.5...
sending incremental file list
base/16384/2619
4.20M 100% 35.20MB/s 0:00:00 (xfr#1, to-chk=210/230)
...
The command prints the progress of rsync. It prints no message when it finishes, and it doesn't check whether each step worked. The exit status is the one of the last command, which restarts PostgreSQL on the remote server.
Every run is logged to /var/log/go/postgresync.log with a timestamp and the arguments used. The log includes the password you typed.
Examples
# Recommended: stop PostgreSQL here while copying
systemctl stop postgresql
/go/postgresync 203.0.113.5 'RemoteRootPassword'
systemctl start postgresql
# The remote SSH server listens on port 2222
/go/postgresync 203.0.113.5 'RemoteRootPassword' 2222
# Check the result on the remote server
ssh root@203.0.113.5 "systemctl status postgresql"
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| PostgreSQL doesn't start on the remote server | The copy is damaged because the files changed during the copy, or the versions differ. | Check journalctl -xeu postgresql on the remote server. Stop PostgreSQL here and run the command again. |
database files are incompatible with server |
The two servers run different major versions of PostgreSQL. | Install the same major version on both. |
rsync: ... No such file or directory |
The data directory isn't /var/lib/pgsql/data/ here (Debian and Ubuntu keep it under /var/lib/postgresql/). |
This command doesn't support that layout. Use a dump instead. |
chown: invalid user 'postgres' |
PostgreSQL isn't installed on the remote server. | Install it there with postgresql. |
Permission denied |
The password is wrong, or the remote server doesn't allow root to log in with a password. |
Check the password. Allow root login there (see permitrootlogin). |
sshpass: command not found |
sshpass isn't installed here. |
Install sshpass. |
Connection refused or a timeout |
Wrong IP address or port, or a firewall blocks the port. | Check the port (the 3rd argument) and open it with allow. |
| The databases that were on the remote server are gone | The copy replaces them. This is by design. | Back up the remote server first. |
| Remote connections to the new server stopped working | The configuration files (postgresql.conf, pg_hba.conf) are copied from this server too. |
Edit them on the remote server. |
| The transfer stopped halfway | The connection dropped. | Run the command again. It sends only what is missing. |
Details
systemctl stop postgresqlon the remote server.rsync -azlh -Pfrom/var/lib/pgsql/data/here to/var/lib/pgsql/data/there. Both paths end with/, so the contents are copied into the same folder.chown -R postgres:postgres /var/lib/pgsql/dataon the remote server.systemctl restart postgresqlon the remote server.
The rsync options:
| Option | Meaning |
|---|---|
-a |
Archive mode: copies subfolders and keeps permissions, dates, owners, and groups. |
-z |
Compresses the data while it travels. |
-l |
Copies symbolic links as links. |
-h |
Shows sizes in a readable form. |
-P |
Shows progress and keeps partly sent files, so an interrupted copy can continue. |
There is no --delete, so files that exist only on the remote server stay there.
Security notes
- The password is passed on the command line, so it can be seen in the process list and is saved in your shell history and in
/var/log/go/postgresync.log. - The SSH connection doesn't check the identity of the remote server (
StrictHostKeyChecking=no). - It logs in as
root. Turn root password logins off again on the remote server afterwards.
Files and requirements
| Item | Used for |
|---|---|
/var/lib/pgsql/data/ |
The PostgreSQL data on both servers. |
sshpass, rsync, ssh |
Sending the password and the files. |
rsync and PostgreSQL on the remote server |
Receiving the files and running the database. |
/var/log/go/postgresync.log |
The command log. |
See also
| Command | Description |
|---|---|
postgresql |
Install and start PostgreSQL |
postgre |
Create, edit, or delete a PostgreSQL database and user |
mysqlsync |
Copy MySQL databases from a local to a remote server |
fsync |
Copy a directory from your local server to a remote server |
serversync |
Back up and migrate a GoAdmio server with all configurations |
permitrootlogin |
Allow root SSH login from a specific IP |