用户工具

站点工具


linux:postgresql

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

两侧同时换到之前的修订记录前一修订版
后一修订版
前一修订版
linux:postgresql [2017/04/15 10:33] – Add change user password/role usage memorylinux:postgresql [2023/12/03 10:24] (当前版本) – 外部编辑 127.0.0.1
行 5: 行 5:
 ===== Install on Ubuntu ===== ===== Install on Ubuntu =====
  
-==== Install PostgreSQL 9.5  ====+==== Install PostgreSQL 11  ====
  
 可查看[[http://www.postgresql.org/download/linux/debian/|官网]]教程添加源安装。  可查看[[http://www.postgresql.org/download/linux/debian/|官网]]教程添加源安装。 
  
-下面以''Debian''''jessie''为例,安装PostgreSQL 9.5(Debian仓库内的PostgreSQL是9.4的)+下面以''Debian'' 10 ''buster''为例,安装PostgreSQL 12
  
 添加源: 添加源:
  
 <code> <code>
-echo "deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main" > /etc/apt/sources.list.d/pgdg.list+echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
 </code> </code>
  
行 20: 行 20:
  
 <code> <code>
-wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | +wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
-sudo apt-key add -+
 sudo apt-get update sudo apt-get update
-sudo apt-get install postgresql-9.5+sudo apt-get install postgresql-12
 </code> </code>
  
行 32: 行 31:
 createuser --interactive createuser --interactive
 </code> </code>
-==== Upgrade PostgreSQL from 9.3 to 9.4  ==== 
  
-由于Ubuntu 14.04官方源内的PostgreSQL是9.3的,如果已经有数据,升级到9.4时要做以下步骤:+==== Upgrade PostgreSQL from 9.6 to 10  ====
  
-Thanks: [[https://gist.github.com/dideler/60c9ce184198666e5ab4|Upgrading PostgreSQL from 9.3 to 9.4 when upgrading Ubuntu 14.04 to 14.10]]+由于 Debian9 官方源内的PostgreSQL9.6的,如果已经有数据,升级到10时要做以下步骤:
  
-要求:同时安装9.39.4的PostgreSQL+Thanks: [[https://gist.github.com/dideler/60c9ce184198666e5ab4|Upgrading PostgreSQL from 9.6 to 10 when upgrading Ubuntu 14.04 to 14.10]] 
 + 
 +要求:同时安装9.610的PostgreSQL
  
 0. 先**备份数据** 0. 先**备份数据**
行 46: 行 46:
 </code> </code>
  
-1. 运行''pg_lsclusters'',保证9.39.4都启动了。+1. 运行''pg_lsclusters'',保证9.610都启动了。
  
 <code> <code>
 $ pg_lsclusters  $ pg_lsclusters 
 Ver Cluster Port Status Owner    Data directory               Log file Ver Cluster Port Status Owner    Data directory               Log file
-9.main    5432 online postgres /var/lib/postgresql/9.3/main /var/log/postgresql/postgresql-9.3-main.log +9.main    5432 online postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log 
-9.4 main    5433 online postgres /var/lib/postgresql/9.4/main /var/log/postgresql/postgresql-9.4-main.log+10 main    5433 online postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log
 </code> </code>
  
-2. 暂停9.4+2. 暂停10
  
 <code> <code>
-sudo pg_dropcluster 9.4 main --stop+sudo pg_dropcluster 10 main --stop
 </code> </code>
  
-3. 迁移9.3数据到最新版本+3. 迁移9.6数据到最新版本
  
 <code> <code>
-sudo pg_upgradecluster 9.main+sudo pg_upgradecluster 9.main
 </code> </code>
  
-4. 此时9.3数据库应该停止''stop''+4. 此时9.6数据库应该停止''stop''
  
 <code> <code>
 $ pg_lsclusters $ pg_lsclusters
 Ver Cluster Port Status Owner    Data directory               Log file Ver Cluster Port Status Owner    Data directory               Log file
-9.main    5433 down   postgres /var/lib/postgresql/9.3/main /var/log/postgresql/postgresql-9.3-main.log +9.main    5433 down   postgres /var/lib/postgresql/9.6/main /var/log/postgresql/postgresql-9.6-main.log 
-9.4 main    5432 online postgres /var/lib/postgresql/9.4/main /var/log/postgresql/postgresql-9.4-main.log+10 main    5432 online postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log
 </code> </code>
  
-5. 移除9.3+5. 移除9.6
  
 <code> <code>
-sudo pg_dropcluster 9.main +sudo pg_dropcluster 9.main 
-sudo aptitude remove postgresql-9.postgresql-client-9.3+sudo aptitude remove postgresql-9.postgresql-client-9.6
 </code> </code>
  
行 87: 行 87:
 Visit [[https://yum.postgresql.org/|PostgreSQL Yum Repository]] and select PostgreSQL version, download repo rpm like this: Visit [[https://yum.postgresql.org/|PostgreSQL Yum Repository]] and select PostgreSQL version, download repo rpm like this:
  
-  wget https://download.postgresql.org/pub/repos/yum/9.5/redhat/rhel-6-x86_64/pgdg-centos95-9.5-3.noarch.rpm +  wget https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm 
-  yum install postgresql95 postgresql95-devel postgresql95-libs postgresql95-server+  yum install postgresql10 postgresql10-devel postgresql10-libs postgresql10-server
  
 ===== Usage ===== ===== Usage =====
行 105: 行 105:
 <code bash> <code bash>
 sudo -u postgres psql -c "ALTER USER <user> with superuser" sudo -u postgres psql -c "ALTER USER <user> with superuser"
 +</code>
 +
 +===== Maintain =====
 +
 +Show size of every PostgreSQL database.[(https://www.peterbe.com/plog/show-size-of-every-postgresql-database)]
 +
 +First, connect to ''postgres'' database:
 +
 +<code bash>
 +psql postgres
 +</code>
 +
 +Then run SQL:
 +
 +<code sql>
 +SELECT pg_database.datname, 
 +  pg_database_size(pg_database.datname), 
 +  pg_size_pretty(pg_database_size(pg_database.datname)) 
 +FROM pg_database ORDER by 2 DESC;
 </code> </code>
  
行 110: 行 129:
  
 复用连接可提高数据库的性能,详见[[pgbouncer|PgBouncer]] 复用连接可提高数据库的性能,详见[[pgbouncer|PgBouncer]]
 +
 +==== 优化配置 ====
 +
 +1. 使用 [[https://pgtune.leopard.in.ua/#/|PGTune]] 生成机器最佳的配置,并放到 ''/etc/postgresql/11/main/conf.d''
 +
 +2. 开启慢查询日志
 +
 +<code ini /etc/postgresql/11/main/conf.d/log.conf>
 +logging_collector = on
 +log_statement = 'ddl'
 +log_filename = 'postgresql-%Y-%m-%d.log'
 +log_min_duration_statement = 300
 +</code>
 +
 +===== Awesome List =====
 +
 +  * [[https://pgroonga.github.io/|PGroonga]] - Make PostgreSQL fast full text search platform for all languages!
linux/postgresql.1492223581.txt.gz · 最后更改: 2023/12/03 10:24 (外部编辑)