安装PostgreSQL数据库

参照了两个博客:第一个博客的下载安装

                          第二个博客的配置以及使用方法

/root/图片/2017-01-04 18-54-13 的屏幕截图.png
/root/图片/2017-01-04 18-54-25 的屏幕截图.png
/root/图片/2017-01-04 18-56-23 的屏幕截图.png
/root/图片/2017-01-04 19-20-09 的屏幕截图.png
/root/图片/2017-01-04 19-20-22 的屏幕截图.png
/root/图片/2017-01-04 19-29-34 的屏幕截图.png
/root/图片/2017-01-04 19-29-49 的屏幕截图.png














http://blog.csdn.net/lk10207160511/article/details/50359549

  • 安装postgresql软件包
sudo yum install postgresql-server postgresql
   
   
  • 1
  • 初始化db
sudo su - postgres
initdb -D /var/lib/pgsql/data
   
   
  • 1
  • 2
  • 启动/停止服务
systemctl status postgresql.service
systemctl start postgresql.service
systemctl stop postgresql.service
   
   
  • 1
  • 2
  • 3
  • 修改配置允许远程访问

修改 /var/lib/pgsql/data/postgresql.conf 文件

listen_addresses ='*'
   
   
  • 1

修改 /var/lib/pgsql/data/pg_hba.conf 文件**

host    all             all             0.0.0.0/0            trust
   
   
  • 1
  • 修改完成后,重新启动服务
systemctl restart postgresql.service
   
   
  • 1
  • 建立用户和数据库
$ sudo su - postgres
$ psql -U postgres -W
$ Password for user postgres: postgres
postgres=# CREATE USER <user> WITH PASSWORD '<password>';
postgres=# CREATE DATABASE test OWNER <user> ENCODING 'UTF8';
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 测试
$ psql -U kongxx -d test
test=> \dt

  • 安装postgresql软件包
sudo yum install postgresql-server postgresql
   
   
  • 1
  • 初始化db
sudo su - postgres
initdb -D /var/lib/pgsql/data
   
   
  • 1
  • 2
  • 启动/停止服务
systemctl status postgresql.service
systemctl start postgresql.service
systemctl stop postgresql.service
   
   
  • 1
  • 2
  • 3
  • 修改配置允许远程访问

修改 /var/lib/pgsql/data/postgresql.conf 文件

listen_addresses ='*'
   
   
  • 1

修改 /var/lib/pgsql/data/pg_hba.conf 文件**

host    all             all             0.0.0.0/0            trust
   
   
  • 1
  • 修改完成后,重新启动服务
systemctl restart postgresql.service
   
   
  • 1
  • 建立用户和数据库
$ sudo su - postgres
$ psql -U postgres -W
$ Password for user postgres: postgres
postgres=# CREATE USER <user> WITH PASSWORD '<password>';
postgres=# CREATE DATABASE test OWNER <user> ENCODING 'UTF8';
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 测试
$ psql -U kongxx -d test
test=> \dt




转载地址:http://blog.csdn.net/p106786860/article/details/49877483


一、在CentOS上安装PostgreSQL数据库


下面我们就以CentOS系统为例,给大家讲解一下PostgreSQL的安装过程。 

1.使用yum安装postgresql 

  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数据库 

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

2.启动postgresql服务 

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

3.查看postgresql的服务状态 

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

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

  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]   

二、连接PostgreSQL数据库 

如果想连接到数据库,需要切换到postgres用户下,然后使用psql连接到数据库中。在该用户下连接数据库,是不需要密码的。 

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

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

2.列出所有的数据库 

  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.退出数据库 

  1. postgres=# \q   
  2. -bash-4.1$    

三、PostgreSQL数据库目录 

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

  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  

四、PostgrepSQL的简单配置 

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

1.修改监听的ip和端口 

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

  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)   

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

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

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

日志收集,一般是打开的 

  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)   

日志目录,一般使用默认值 

  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   

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

  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.内存参数的配置 

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

  1. # - Memory -   
  2. shared_buffers = 32MB                   # min 128kB   
  3.                                         # (change requires restart)   

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

  1. # actively intend to use prepared transactions.   
  2. #work_mem = 1MB                         # min 64kB   


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值