职业:dba ,工作时间:5年, 先后从事ERP和数据库工程师工作,熟悉Oracle、MySQL、PostgreSQL数据库,具有 Oracle OCP、MySQL OCP、PostgreSQL PGCE认证,以及红帽Linux架构师认证(RHCA)。
和任何其他数据库管理员或数据库专家一样,我们通常都需要在Unix系统上进行大量的操作。以下是一些我经常使用的命令,我认为应该与其他Postgres成员分享。熟练地使用这些命令可以节省大量时间和精力。 可以将这些命令用作别名(要创建别名,请参阅我的上一篇文章(Postgres DBA和开发常用的Linux/Unix别名,地址:http://www.postgresql-blog.com/most-useful-linux-or-unix-alias-for-postgres-dba-and-developer/)l 按 “Control + z”将进程放置到后台运行[root@Postgres]# nohup root/generate_report.sh & [1] 22401 [root@Postgres]# nohup: ignoring input and appending output to nohup.out’ [root@Postgres]# ps -ef | grep report root 22401 22251 99 02:22 pts/0 00:00:06 bin/bash root/generate_report.sh root 22409 22251 0 02:22 pts/0 00:00:00 grep "color=auto report [root@Postgres]#[root@Postgres]# bash generate_report.sh[root@Postgres]# ps -ef | grep report root 22401 22251 99 02:22 pts/0 00:00:06 bin/bash root/generate_report.sh root 22409 22251 0 02:22 pts/0 00:00:00 grep "color=auto report [root@Postgres]#注意:仅删除不同的邮件队列消息(即仅删除系统打算稍后重试的消息)echo "<This is the message body>" | mutt -a <file_name> -s "Notification_Subject" <email id> echo "From 'hostname' server the attachment" |mailx -s "mailx test mail" -a /proc/meminfo xxx@gmail.comfind . -type f -exec grep -l "word" {} +find / -type f -exec grep -H 'text-to-find-here' {} \;[root@ip-172-31-46-80 data]# pwd /var/lib/pgsql/11/data [root@ip-172-31-46-80 data]# find . -type f -exec grep -H 'max' {} \; ./log/postgresql-Thu.log:2019-11-21 18:42:45.925 UTC [28207] HINT: Consider increasing the configuration parameter "max_wal_size". ./log/postgresql-Thu.log:2019-11-21 18:43:12.014 UTC [28207] HINT: Consider increasing the configuration parameter "max_wal_size". ./postgresql.conf:max_connections = 100 # (change requires restart) ./postgresql.conf:#max_prepared_transactions = 0 # zero disables the feature ./postgresql.conf:# Caution: it is not advisable to set max_prepared_transactions nonzero unlessfind /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 {} ;tar -cf packed_files.tar file_to_pack1 file_to_pack2 ...tar -cf - file_to_pack1 file_to_pack2 ... | gzip -c > packed_files.tar.gz# 要从目录及其子目录创建tar文件,请执行以下操作:tar -cvf packed_files.tar dir_to_packtar -xvf file_to_unpack.targunzip packed_files.tar.gz tar -xf packed_files.targunzip -c packed_files.tar.gz | tar -xf -tar -tvf file_to_list.tar注意:要使用bzip2代替gzip,只需将上面的命令替换为bzip2(使用gzip)和bunzip2(使用gunzip)。“ diff”命令显示file1和file2之间的差异。如:diff README.txt README2.txtdiff -y file1 file2 -W 120“ rsync”是用于同步文件的好帮手,以下是一些示例:rsync -P rsync://rsync.server.com/path/to/file filersync --bwlimit=1000 fromfile tofile rsync -az -e ssh --delete ~/public_html/ remote.com:'~/public_html'rsync -auz -e ssh remote:/dir/ . && rsync -auz -e ssh . remote:/dir/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 \;您还可以在Ubuntu / Debian / Fedora和其他基于Linux的发行版下使用sudo命令:最好向所有已登录的用户提供系统正在关闭的通知,并在TIME的最后五分钟内阻止新的登录。键入以下命令:shutdown -r +5 Broadcast message from ajay@ip-172-31-46-80 (/dev/pts/1) at 13:21 ... The system is going down for reboot in 5 minutes!
描述 | Unix 命令 |
在后台运行程序 | nohup |
查看邮件列表 | mailq -i |
查看邮件的详细内容 | postcat -vq XXXXXXXXXX |
使用Postfix立即处理队列 | postqueue -f |
使用Postfix立即尝试发送所有排队的消息 | postfix flush |
发送电子邮件时不加上附件 | echo “<message body>” | mutt -a <file_name> \ -s “Notification_Subject” <email id> |
发送电子邮件时加上附件 | echo “From ‘hostname’ server the attachment” \ |mailx -s “mailx test mail” -a /proc/meminfo\ xxx@gmail.com |
根据指定的字符串查找文件 | find . -type f -exec grep -l “word” {} + |
打印带有匹配项的文件名(仅适用于文本文件) | find / -type f -exec grep -H ‘text-to-find-here’ {} \; |
删除x天之前的文件 | find /path/to/files* -mtime +5 -exec rm {} \; |
查找x天之前的文件 | find /var/arhive_backup -mtime +5 -exec ls -l {} \; |
查找并删除文件 | find /backup/logs/ -name daily_backup* -mtime +21\ -exec rm -f {} ; |
分步执行打包 | tar -cf packed_files.tar file_to_pack1 file_to_pack2 … gzip packed_files.tar |
一步完成打包 | tar -cf – file_to_pack1 file_to_pack2 … | \ gzip -c > packed_files.tar.gz |
从目录及其子目录创建tar文件 | tar -cvf packed_files.tar dir_to_pack |
解压tar文件 | tar -xvf file_to_unpack.tar |
分步打包文件 | gunzip packed_files.tar.gz tar -xf packed_files.tar |
一次性打包文件 | gunzip -c packed_files.tar.gz | tar -xf – |
查看tar文件内容 | tar -tvf file_to_list.tar |
查看2个文件的差异 | diff README.txt README2.txt |
显示相同的输出,但并排显示 | diff -y file1 file2 -W 120 |
同步远程文件 | rsync -P rsync://rsync.server.com/path/to/file file |
本地复制并进行速率限制 | rsync –bwlimit=1000 fromfile tofile |
镜像网站(使用压缩和加密) | rsync -az -e ssh –delete ~/public_html/ \remote.com:’~/public_html |
将当前目录与远程目录同步 | rsync -auz -e ssh remote:/dir/ . &&\ rsync -auz -e ssh . remote:/dir/ |
查找时间戳之间的所有文件并将其移动到新位置 | find . -newermt “2015-09-26 00:00:00” ! \ -newermt “2015-09-26 11:59:59” -exec cp {} /var/lib/pgsql/9.3/cluster2data2/log_abc \; |
重启系统 | # /sbin/reboot |
Ubuntu/Debian/Fedora重启系统 | sudo reboot |
在TIME的最后五分钟内阻止新的登录 | # shutdown -r +5 |
1. 适用于Postgres管理员或开发人员的性能最佳命令2. 用于Postgres性能故障排除的常用的GUI工具
PostgreSQL中文社区欢迎广大技术人员投稿
投稿邮箱:press@postgres.cn
最后修改时间:2020-09-25 17:28:00
文章转载自
PostgreSQL中文社区,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。