PostgreSQL:PostgreSQL的安装和配置

其实我已经原文copy 原文连接:https://blog.csdn.net/p106786860/article/details/49877483sql


1、在CentOS上安装PostgreSQL数据库 数据库

下面咱们就以CentOS系统为例,给你们讲解一下PostgreSQL的安装过程。 json

1.使用yum安装postgresql 缓存

[plain]  view plain  copy
  1. [pengchengxiang@localhost ~]$ sudo yum install postgresql-server.x86_64   
  2. Loaded plugins: fastestmirror, refresh-packagekit, security   
  3. Setting up Install Process   
  4. Loading mirror speeds from cached hostfile   
  5.  * base: mirrors.btte.net   
  6.  * extras: mirror.bit.edu.cn   
  7.  * updates: mirror.bit.edu.cn   
  8. Resolving Dependencies   
  9. --> Running transaction check   
  10. ---> Package postgresql-server.x86_64 0:8.4.20-3.el6_6 will be installed   
  11. --> Processing Dependency: postgresql(x86-64) = 8.4.20-3.el6_6 for package: postgresql-server-8.4.20-3.el6_6.x86_64   
  12. --> Running transaction check   
  13. ---> Package postgresql.x86_64 0:8.4.20-3.el6_6 will be installed   
  14. --> Finished Dependency Resolution   
  15. Dependencies Resolved   
  16. ================================================================================   
  17.  Package                 Arch         Version               Repository     Size   
  18. ================================================================================   
  19. Installing:   
  20.  postgresql-server       x86_64       8.4.20-3.el6_6        updates       3.4 M   
  21. Installing for dependencies:   
  22.  postgresql              x86_64       8.4.20-3.el6_6        updates       2.6 M   
  23. Transaction Summary   
  24. ================================================================================   
  25. Install       2 Package(s)   
  26. Total download size: 6.0 M   
  27. Installed size: 28 M   
  28. Is this ok [y/N]: y   
  29. Downloading Packages:   
  30. (1/2): postgresql-8.4.20-3.el6_6.x86_64.rpm              | 2.6 MB     00:02        
  31. (2/2): postgresql-server-8.4.20-3.el6_6.x86_64.rpm       | 3.4 MB     00:06        
  32. --------------------------------------------------------------------------------   
  33. Total                                           680 kB/s | 6.0 MB     00:09        
  34. Running rpm_check_debug   
  35. Running Transaction Test   
  36. Transaction Test Succeeded   
  37. Running Transaction   
  38.   Installing : postgresql-8.4.20-3.el6_6.x86_64                             1/2    
  39.   Installing : postgresql-server-8.4.20-3.el6_6.x86_64                      2/2    
  40.   Verifying  : postgresql-8.4.20-3.el6_6.x86_64                             1/2    
  41.   Verifying  : postgresql-server-8.4.20-3.el6_6.x86_64                      2/2    
  42. Installed:   
  43.   postgresql-server.x86_64 0:8.4.20-3.el6_6                                        
  44. Dependency Installed:   
  45.   postgresql.x86_64 0:8.4.20-3.el6_6                                               
  46. Complete!   

2.初始化postgresql数据库 bash

[plain]  view plain  copy
  1. [pengchengxiang@localhost ~]$ sudo service postgresql initdb   
  2. Initializing database: [  OK  ]   

2.启动postgresql服务 app

[plain]  view plain  copy
  1. [pengchengxiang@localhost ~]$ sudo service postgresql start   
  2. Starting postgresql service: [  OK  ]   

3.查看postgresql的服务状态 ide

[plain]  view plain  copy
  1. [pengchengxiang@localhost ~]$ sudo service postgresql status   
  2. postmaster (pid  3496) is running...   

问题:若是你在没有进行初始化数据库以前就启东postgrepsql服务,则会报错以下: post

[plain]  view plain  copy
  1. [pengchengxiang@localhost ~]$ sudo service postgresql start   
  2. /var/lib/pgsql/data is missing. Use "service postgresql initdb" to initialize the cluster first.   
  3. [FAILED]   

2、链接PostgreSQL数据库 ui

若是想链接到数据库,须要切换到postgres用户下,而后使用psql链接到数据库中。在该用户下链接数据库,是不须要密码的。 this

1.切换的postgres用户,并链接数据库 

[plain]  view plain  copy
  1. [pengchengxiang@localhost ~]$ sudo su - postgres   
  2. -bash-4.1$ psql   
  3. psql (8.4.20)   
  4. Type "help" for help.   
  5. postgres=#    

2.列出全部的数据库 

[plain]  view plain  copy
  1. postgres=# \l   
  2.                                   List of databases   
  3.    Name    |  Owner   | Encoding |  Collation  |    Ctype    |   Access privileges      
  4. -----------+----------+----------+-------------+-------------+-----------------------   
  5.  postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |    
  6.  template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   
  7.                                                              : postgres=CTc/postgres   
  8.  template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres   
  9.                                                              : postgres=CTc/postgres   
  10. (3 rows)   

3.退出数据库 

[plain]  view plain  copy
  1. postgres=# \q   
  2. -bash-4.1$    

3、PostgreSQL数据库目录 

默认安装上,PostgreSQL的数据库目录在/var/lib/pgsql/data目录。 

[plain]  view plain  copy
  1. -bash-4.1$ ls -l /var/lib/pgsql/data/   
  2. total 80   
  3. drwx------. 5 postgres postgres  4096 Nov 16 23:55 base   
  4. drwx------. 2 postgres postgres  4096 Nov 16 23:55 global   
  5. drwx------. 2 postgres postgres  4096 Nov 16 23:55 pg_clog   
  6. -rw-------. 1 postgres postgres  3411 Nov 16 23:55 pg_hba.conf   
  7. -rw-------. 1 postgres postgres  1631 Nov 16 23:55 pg_ident.conf   
  8. drwx------. 2 postgres postgres  4096 Nov 17 00:00 pg_log   
  9. drwx------. 4 postgres postgres  4096 Nov 16 23:55 pg_multixact   
  10. drwx------. 2 postgres postgres  4096 Nov 17 00:02 pg_stat_tmp   
  11. drwx------. 2 postgres postgres  4096 Nov 16 23:55 pg_subtrans   
  12. drwx------. 2 postgres postgres  4096 Nov 16 23:55 pg_tblspc   
  13. drwx------. 2 postgres postgres  4096 Nov 16 23:55 pg_twophase   
  14. -rw-------. 1 postgres postgres     4 Nov 16 23:55 PG_VERSION   
  15. drwx------. 3 postgres postgres  4096 Nov 16 23:55 pg_xlog   
  16. -rw-------. 1 postgres postgres 16886 Nov 16 23:55 postgresql.conf   
  17. -rw-------. 1 postgres postgres    57 Nov 16 23:55 postmaster.opts   
  18. -rw-------. 1 postgres postgres    45 Nov 16 23:55 postmaster.pid  

4、PostgrepSQL的简单配置 

PostgreSQL数据库的配置主要是经过修改数据目录下的postgresql.conf文件来实现的。 

1.修改监听的ip和端口 

使用postgresql用户链接数据库后,进入到/var/lib/pgsql/data目录下,编辑postgresql.conf文件: 

[plain]  view plain  copy
  1. # - Connection Settings -   
  2. #listen_addresses = '*'                 # what IP address(es) to listen on;   
  3.                                         # comma-separated list of addresses;   
  4.                                         # defaults to 'localhost', '*' = all   
  5.                                         # (change requires restart)   
  6. #port = 5432                            # (change requires restart)   

修改这两个参数以后,须要重启以后才能生效 

[plain]  view plain  copy
  1. [pengchengxiang@localhost ~]$ sudo service postgresql restart   
  2. Stopping postgresql service: [  OK  ]   
  3. Starting postgresql service: [  OK  ]   

2.修改数据库log相关的参数 

日志收集,通常是打开的 

[plain]  view plain  copy
  1. # This is used when logging to stderr:   
  2. logging_collector = on                  # Enable capturing of stderr and csvlog   
  3.                                         # into log files. Required to be on for   
  4.                                         # csvlogs.   
  5.                                         # (change requires restart)   

日志目录,通常使用默认值 

[plain]  view plain  copy
  1. # These are only used if logging_collector is on:   
  2. log_directory = 'pg_log'                # directory where log files are written,   
  3.                                         # can be absolute or relative to PGDATA   

只保留一天的日志,进行循环覆盖 

[plain]  view plain  copy
  1. log_filename = 'postgresql-%a.log'      # log file name pattern,   
  2.                                         # can include strftime() escapes   
  3. log_truncate_on_rotation = on           # If on, an existing log file of the   
  4.                                         # same name as the new log file will be   
  5.                                         # truncated rather than appended to.   
  6.                                         # But such truncation only occurs on   
  7.                                         # time-driven rotation, not on restarts   
  8.                                         # or size-driven rotation.  Default is   
  9.                                         # off, meaning append to existing files   
  10.                                         # in all cases.   
  11. log_rotation_age = 1d                   # Automatic rotation of logfiles will   
  12.                                         # happen after that time.  0 disables.   
  13. log_rotation_size = 0                   # Automatic rotation of logfiles will   

3.内存参数的配置 

共享内存的大小,用于共享数据块。若是你的机器上有足够的内存,能够把这个参数改的大一些,这样数据库就能够缓存更多的数据块,当读取数据时,就能够从共享内存中读,而不须要再从文件上去读取。 

[plain]  view plain  copy
  1. # - Memory -   
  2. shared_buffers = 32MB                   # min 128kB   
  3.                                         # (change requires restart)   

单个SQL执行时,排序、hash json所用的内存,SQL运行完后,内存就释放了。 

[plain]  view plain  copy
  1. # actively intend to use prepared transactions.   
  2. #work_mem = 1MB                         # min 64kB