Essential Unix Commands Every PostgreSQL DBA Should Know
This guide compiles the most useful Unix/Linux commands for PostgreSQL administrators, covering background execution, mail queue handling, email with attachments, file searching, cleanup, compression, diffing, remote synchronization, timestamp‑based file moves, and system reboot techniques.
1. Run programs in the background
Use nohup to detach a script from the terminal and keep it running after logout: nohup /root/generate_report.sh & Alternatively, press Ctrl+Z to suspend a running process and then type bg to resume it in the background. Verify with:
ps -ef | grep report2. Inspect and manage the mail queue (Postfix)
mailq -i– display the mail queue. postcat -vq <message-id> – view the full content of a specific message. postqueue -f – force immediate processing of the queue. postfix flush – attempt to send all queued mail right away. postsuper -d ALL – delete all queued messages (only those pending retry).
3. Send email with attachments
Using mutt:
echo "<This is the message body>" | mutt -a <file_name> -s "Notification_Subject" <email_address>Using mailx:
echo "From 'hostname' server the attachment" | mailx -s "mailx test mail" -a /proc/meminfo [email protected]4. Find files containing a specific string
find . -type f -exec grep -l "word" {} +– list only file names that contain the word. find / -type f -exec grep -H 'text-to-find-here' {} \; – print matching file names with the matching line.
Example on PostgreSQL logs:
find . -type f -exec grep -H 'max' {} \;5. Locate and delete files older than X days
find /path/to/files* -mtime +5 -exec rm {} \; find /var/arhive_backup -mtime +5 -exec ls -l {} \; find /backup/logs/ -name 'daily_backup*' -mtime +21 -exec rm -f {} \;6. Compress and extract archives
Step‑by‑step packing:
tar -cf packed_files.tar file1 file2 ... gzip packed_files.tarOne‑liner packing: tar -cf - file1 file2 ... | gzip -c > packed_files.tar.gz Create a tar from a directory: tar -cvf packed_files.tar dir_to_pack Extract a tar archive: tar -xvf file_to_unpack.tar Two‑step extraction:
gunzip packed_files.tar.gz tar -xf packed_files.tarOne‑command extraction: gunzip -c packed_files.tar.gz | tar -xf - List contents of a tar file: tar -tvf file_to_list.tar Replace gzip with bzip2 / bunzip2 if desired.
7. Compare two files
Basic diff: diff README.txt README2.txt Ignore whitespace: diff -t file1 file2 Ignore case: diff -i file1 file2 Side‑by‑side view:
diff -y file1 file2 -W 1208. Synchronize directories remotely with rsync
Copy only differences: rsync -P rsync://rsync.server.com/path/to/file file Rate‑limited local copy: rsync --bwlimit=1000 fromfile tofile Mirror a website (compressed & encrypted):
rsync -az -e ssh --delete ~/public_html/ remote.com:'~/public_html'Sync current directory with remote and back:
rsync -auz -e ssh remote:/dir/ . && rsync -auz -e ssh . remote:/dir/9. Move files between timestamps
Find files created between two dates and copy them:
find . -newermt "2015-09-26 00:00:00" ! -newermt "2015-09-26 11:59:59" -exec cp {} /var/lib/pgsql/9.3/cluster2data2/log_edb \;10. Reboot the system
/sbin/reboot– immediate reboot. shutdown -r +5 – schedule reboot in 5 minutes (gives users warning). sudo reboot – reboot on Ubuntu/Debian/Fedora with sudo.
It is good practice to broadcast a shutdown message and block new logins during the final minutes.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
