Skip to content

Commit 8b17321

Browse files
committed
docs: add postgres upgrade description
1 parent 103a61d commit 8b17321

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

docs/postgres.md

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,146 @@ List of key-value tables/relations:
6767
```
6868

6969
Native SQL tables include invoices and graph data, representing the ongoing migration toward a fully native SQL implementation.
70+
71+
## Upgrading Postgres Version
72+
73+
This guide describes the recommended approach for upgrading your Postgres database to a newer version. This process requires planned downtime for your LND node.
74+
75+
### Prerequisites
76+
77+
- Access to both old and new Postgres instances
78+
- Sufficient disk space for database dumps
79+
- Administrative access to stop/start LND
80+
81+
### Migration Steps
82+
83+
#### 1. Prepare the New Postgres Instance
84+
85+
Install and configure the new Postgres version on your target system. Ensure the new instance is running and accessible.
86+
87+
Create an empty database for LND on the new Postgres instance:
88+
89+
```shell
90+
$ psql -h new-postgres-host -U postgres
91+
postgres=# CREATE DATABASE lnddb;
92+
postgres=# CREATE USER lnduser WITH PASSWORD 'your-password';
93+
postgres=# GRANT ALL PRIVILEGES ON DATABASE lnddb TO lnduser;
94+
```
95+
96+
#### 2. Stop LND
97+
98+
Before beginning the migration, stop your LND node to ensure data consistency:
99+
100+
```shell
101+
$ lncli stop
102+
```
103+
104+
Verify that LND has fully stopped before proceeding.
105+
106+
#### 3. Backup the Current Database
107+
108+
Create a full backup of your current database using `pg_dump`:
109+
110+
```shell
111+
$ pg_dump -h old-postgres-host -U lnduser -d lnddb -F c -f lnddb_backup.dump
112+
```
113+
114+
The `-F c` flag creates a custom format dump which is recommended for `pg_restore`. This backup serves as your safety net during the migration.
115+
116+
**Important**: Store this backup in a safe location. Without a backup, you cannot recover if something goes wrong during the migration.
117+
118+
#### 4. Restore to New Postgres Instance
119+
120+
Restore the database to your new Postgres instance:
121+
122+
```shell
123+
$ pg_restore -h new-postgres-host -U lnduser -d lnddb -v --on-conflict-do-nothing --exit-on-error lnddb_backup.dump
124+
```
125+
126+
The `-v` flag provides verbose output so you can monitor the restoration progress.
127+
128+
**Critical**: Always use the `--exit-on-error` flag during restoration. Without this flag, `pg_restore` may continue even if errors occur, which can lead to incomplete schema migration and data inconsistencies. The restore process must fail fast if any errors are encountered to ensure data integrity.
129+
130+
#### 5. Verify the Migration
131+
132+
After restoration completes, perform basic verification checks:
133+
134+
```shell
135+
$ psql -h new-postgres-host -U lnduser -d lnddb
136+
```
137+
138+
Check that all expected tables exist:
139+
140+
```sql
141+
-- Verify key-value tables
142+
SELECT tablename FROM pg_tables WHERE schemaname = 'public' AND tablename LIKE '%_kv';
143+
144+
-- Check row counts for a sample key-value table (e.g., channeldb_kv)
145+
SELECT COUNT(*) FROM channeldb_kv;
146+
147+
-- Check row counts for native SQL tables (if applicable two example tables are
148+
-- depicted in the following)
149+
SELECT COUNT(*) FROM invoices;
150+
SELECT COUNT(*) FROM graph_nodes;
151+
```
152+
153+
Compare the row counts with the old database to ensure data was migrated successfully.
154+
155+
#### 6. Update LND Configuration
156+
157+
Update your LND configuration to point to the new Postgres instance:
158+
159+
```
160+
[db]
161+
db.backend=postgres
162+
db.postgres.dsn=postgresql://lnduser:your-password@new-postgres-host:5432/lnddb
163+
db.postgres.timeout=0
164+
```
165+
166+
#### 7. Start LND and Verify
167+
168+
Start your LND node:
169+
170+
```shell
171+
$ lnd
172+
```
173+
174+
Monitor the logs during startup to ensure LND connects successfully to the new database. Verify basic functionality:
175+
176+
```shell
177+
$ lncli getinfo
178+
$ lncli listchannels
179+
$ lncli listinvoices
180+
```
181+
182+
If LND starts successfully and you can query your channels and invoices, the migration is complete.
183+
184+
### Troubleshooting
185+
186+
If LND fails to start or you encounter issues:
187+
188+
1. Check LND logs for database connection errors
189+
2. Verify the connection string in your LND configuration
190+
3. Ensure the new Postgres instance is accessible from your LND host
191+
4. Verify that all tables were restored correctly in the new database
192+
193+
### Post-Migration
194+
195+
Once you've verified everything is working correctly:
196+
197+
1. Keep the backup file (`lnddb_backup.dump`) for at least a few days
198+
2. Monitor your LND node closely for the first 24-48 hours
199+
3. The old Postgres instance can be decommissioned once you're confident in the new setup
200+
201+
**Note**: There is no automated rollback procedure. If you need to revert, you must restore from your backup to the old Postgres instance and reconfigure LND.
202+
203+
### Replication Considerations
204+
205+
If you are running Postgres with replication (as discussed in the "Important note about replication" section), additional steps may be needed:
206+
207+
1. Ensure replication is properly configured on the new Postgres instance before starting LND
208+
2. Verify that synchronous replication is working correctly after the migration
209+
3. Test failover scenarios to ensure the replica has been correctly synchronized
210+
4. Monitor replication lag to ensure replicas stay in sync with the primary
211+
212+
**Note**: Always consult and follow the official PostgreSQL best practices for version upgrades and database migrations.

0 commit comments

Comments
 (0)