This is an internal documentation. There is a good chance you’re looking for something else. See Disclaimer.
Copy/Dump/Restore Database¶
Hint
If you want restore a backup have a look at Database Backups.
Copy Database¶
Hint
For the special host
"localhost"
to work properly, Postgres must be set up as described in Setup Postgres.
Examples:
Copy DB of installations abc to server where abctest resides naming the new db nice_abctest_new:
tco cp abc abctest/nice_abctest_new
Copy prod DB of bbg to localhost:
tco cp bbg
localhost is default if no target is specified.
See tco --help
for more examples and additional information.
Copy Database Using WITH TEMPLATE¶
This is the fastest way to copy a database. Alternatively, you can dump and then restore the database.
CREATE DATABASE ${TARGET_DB} WITH TEMPLATE ${SOURCE_DB};
Warning
This requires that no one is connected to the database. Consequently, it isn’t possible to copy a database of a running system.
Example¶
switch to the right project
oc project nice-${INSTALLATION}
check how many instance are running
oc get dc/nice -o go-template='{{.spec.replicas}}{{"\n"}}'
stop instance (if required)
oc scale --replicas=0 dc/nice
copy database
sql CREATE DATABASE ${NAME_OF_DB_COPY} WITH TEMPLATE ${SOURCE_DB_NAME};
Hint
If you get “source database ‘…’ is being accessed by other users”, try killing the connections to the database first.
Note
By convention, databases not used by a test or production systems should follow this naming pattern:
nice_${CUSTOMER}_${YOUR_SHORT_NAME}_${YEAR}${MONTH}${DAY}
restart instances (if previously stopped)
oc scale --replicas=${N} dc/nice
Start ${N} instances.
Dump Database¶
tco db-backup dump ${INSTALLATION}
or
tco db-backup dump ${DB_SERVER}/${DB_NAME}
Hint
The restore command is printed by tco db-backup dump
. Use
it to restore the dump later.
Restore command can be used for several days before the backup is removed after archiving it via Borg and Burp. Talk to Operations you need a backup restored from there.
Restore Database¶
Restore Backup¶
For restoring backups, including those made via tco db-backup
,
see Database Backups
Restore Dump File:
Create an empty database:
CREATE DATABASE ${DB_NAME} WITH OWNER ${DB_USER};
Restore database
Regular *.psql/*.dump files or directories:
pg_restore -j 4 --role ${DB_USER} --no-owner --no-acl -d ${DB_NAME} ${DUMP_FILE_OR_DIRECTORY}
*.zstd files:
zstd -qcd ${DUMP_FILE} | pg_restore --role ${DB_USER} --no-owner --no-acl -d ${DB_NAME}
run ANALYZE [1]:
ANALYZE
Note
If needed, adjust owner of DB and content:
REASSIGN OWNED BY ${OLD_DB_USER} TO ${NEW_DB_USER};
Warning
REASSIGN OWNED
will change the owner of all objects in the DB connected to
matching BY ${ROLE_NAME}
and the owner of all DBs matching. Be careful when
there are other DBs that have ${ROLE_NAME}
as owner!
Footnotes