linux安装postgresql并启动服务|linux安装PostgreSQL并启动和关闭教程

更新时间:2019-11-18    来源:PostgreSQL    手机版     字体:

【www.bbyears.com--PostgreSQL】

操作系统版本


[root@web103 ~]# more /etc/issue
CentOS release 5.9 (Final)
Kernel \r on an \m
[root@web103 ~]# uname -a
Linux web103 2.6.18-348.el5 #1 SMP Tue Jan 8 17:53:53 EST 2013 x86_64 x86_64 x86_64 GNU/Linux

下载对应PostgreSQL对应rpm包
因为操作系统版本为CentOS 5.9的64位Linux,因此下载对应版本prm包,主要下载了server,client,contrib,libs四个包


[root@web103 ~]# mkdir pg
[root@web103 ~]# cd pg
[root@web103 pg]# wget http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm
--2015-06-16 20:44:52--  http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm
Resolving yum.postgresql.org... 174.143.35.196, 2001:4800:1501:1::196
Connecting to yum.postgresql.org|174.143.35.196|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1807607 (1.7M) [application/x-redhat-package-manager]
Saving to: `postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm'
%[===================================================================================================================>] 1,807,607   73.6K/s   in 30s     
-06-16 20:45:24 (58.1 KB/s) - `postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm' saved [1807607/1807607]

[root@web103 pg]# wget http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-server-9.4.4-1PGDG.rhel5.x86_64.rpm
--2015-06-16 20:45:35--  http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-server-9.4.4-1PGDG.rhel5.x86_64.rpm
Resolving yum.postgresql.org... 174.143.35.196, 2001:4800:1501:1::196
Connecting to yum.postgresql.org|174.143.35.196|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 6175991 (5.9M) [application/x-redhat-package-manager]
Saving to: `postgresql94-server-9.4.4-1PGDG.rhel5.x86_64.rpm'
%[===================================================================================================================>] 6,175,991   58.5K/s   in 2m 4s   
-06-16 20:47:42 (48.6 KB/s) - `postgresql94-server-9.4.4-1PGDG.rhel5.x86_64.rpm' saved [6175991/6175991]

[root@web103 pg]# wget http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-contrib-9.4.4-1PGDG.rhel5.x86_64.rpm
--2015-06-16 20:47:51--  http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-contrib-9.4.4-1PGDG.rhel5.x86_64.rpm
Resolving yum.postgresql.org... 174.143.35.196, 2001:4800:1501:1::196
Connecting to yum.postgresql.org|174.143.35.196|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664051 (648K) [application/x-redhat-package-manager]
Saving to: `postgresql94-contrib-9.4.4-1PGDG.rhel5.x86_64.rpm'
%[===================================================================================================================>] 664,051     28.1K/s   in 53s     
-06-16 20:48:46 (12.3 KB/s) - `postgresql94-contrib-9.4.4-1PGDG.rhel5.x86_64.rpm' saved [664051/664051]

[root@web103 pg]# wget http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-libs-9.4.4-1PGDG.rhel5.x86_64.rpm
--2015-06-16 20:51:10--  http://yum.postgresql.org/9.4/redhat/rhel-5-x86_64/postgresql94-libs-9.4.4-1PGDG.rhel5.x86_64.rpm
Resolving yum.postgresql.org... 174.143.35.196, 2001:4800:1501:1::196
Connecting to yum.postgresql.org|174.143.35.196|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 233206 (228K) [application/x-redhat-package-manager]
Saving to: `postgresql94-libs-9.4.4-1PGDG.rhel5.x86_64.rpm'
%[===================================================================================================================>] 233,206     70.3K/s   in 3.2s    
-06-16 20:51:16 (70.3 KB/s) - `postgresql94-libs-9.4.4-1PGDG.rhel5.x86_64.rpm' saved [233206/233206]

[root@web103 pg]# ls
postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm          postgresql94-libs-9.4.4-1PGDG.rhel5.x86_64.rpm
postgresql94-contrib-9.4.4-1PGDG.rhel5.x86_64.rpm  postgresql94-server-9.4.4-1PGDG.rhel5.x86_64.rpm

安装PostgreSQL rpm包


[root@web103 pg]# rpm -ivh *.rpm
warning: postgresql94-9.4.4-1PGDG.rhel5.x86_64.rpm: Header V3 DSA signature: NOKEY, key ID 442df0f8
Preparing...                ########################################### [100%]
:postgresql94-libs      ########################################### [ 25%]
:postgresql94           ########################################### [ 50%]
:postgresql94-contrib   ########################################### [ 75%]
:postgresql94-server    ########################################### [100%]

创建PostgreSQL 默认库


[root@web103 pg]# service postgresql-9.4 initdb
Initializing database: [  OK  ]
[root@web103 data]# pwd
/var/lib/pgsql/9.4/data
[root@web103 data]# ls -ltr
total 120
-rw------- 1 postgres postgres 21265 Jun 16 20:52 postgresql.conf
-rw------- 1 postgres postgres    88 Jun 16 20:52 postgresql.auto.conf
drwx------ 3 postgres postgres  4096 Jun 16 20:52 pg_xlog
-rw------- 1 postgres postgres     4 Jun 16 20:52 PG_VERSION
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_twophase
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_tblspc
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_subtrans
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_snapshots
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_serial
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_replslot
drwx------ 4 postgres postgres  4096 Jun 16 20:52 pg_multixact
drwx------ 4 postgres postgres  4096 Jun 16 20:52 pg_logical
-rw------- 1 postgres postgres  1636 Jun 16 20:52 pg_ident.conf
-rw------- 1 postgres postgres  4224 Jun 16 20:52 pg_hba.conf
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_dynshmem
drwx------ 2 postgres postgres  4096 Jun 16 20:52 pg_clog
drwx------ 5 postgres postgres  4096 Jun 16 20:52 base
drwx------ 2 postgres postgres  4096 Jun 16 21:16 pg_log
drwx------ 2 postgres postgres  4096 Jun 16 21:16 global
-rw------- 1 postgres postgres    80 Jun 16 21:39 postmaster.pid
-rw------- 1 postgres postgres    59 Jun 16 21:39 postmaster.opts
drwx------ 2 postgres postgres  4096 Jun 16 21:39 pg_stat
drwx------ 2 postgres postgres  4096 Jun 16 21:39 pg_notify
drwx------ 2 postgres postgres  4096 Jun 16 22:00 pg_stat_tmp

另外还可以通过如下两种方式创建


initdb -D /var/lib/pgsql/9.4/data
pg_ctl -D /var/lib/pgsql/9.4/data

设置PostgreSQL 开机自动启动


[root@web103 pg]# chkconfig postgresql-9.4 on
[root@web103 pg]# chkconfig --list|grep post
postgresql-9.4  0:off   1:off   2:on    3:on    4:on    5:on    6:off

查看默认创建PostgreSQL 用户


[root@web103 data]# more /etc/passwd|grep post
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash

增加PATH环境变量


-bash-3.2$echo "PATH=$PATH:/usr/pgsql-9.4/bin;export PATH" >>~/.bash_profile

启动PostgreSQL 数据库


--方法1
-bash-3.2$ postgres -D /var/lib/pgsql/9.4/data
----或者
-bash-3.2$ postgres -D /var/lib/pgsql/9.4/data >~/pg.log 2>&1 &

方法2
pg_ctl start -l ~/pg.log -D /var/lib/pgsql/9.4/data

这里如果在环境变量中配置了PGDATA,那-D也可以不指定,-l为指定日志目录,建议使用封装的方法2启动pg

查看PostgreSQL进程信息


[root@web103 data]# ps -ef|grep post|grep -v grep
postgres  4432     1  0 21:39 ?        00:00:00 /usr/pgsql-9.4/bin/postgres -D /var/lib/pgsql/9.4/data
postgres  4433  4432  0 21:39 ?        00:00:00 postgres: logger process       
postgres  4435  4432  0 21:39 ?        00:00:00 postgres: checkpointer process   
postgres  4436  4432  0 21:39 ?        00:00:00 postgres: writer process          
postgres  4437  4432  0 21:39 ?        00:00:00 postgres: wal writer process     
postgres  4438  4432  0 21:39 ?        00:00:00 postgres: autovacuum launcher process  
postgres  4439  4432  0 21:39 ?        00:00:00 postgres: stats collector process

这里可以看到pg也和oracle有几分类似,有日志进程,checkpoint进程,有写进程等(具体以后分析)

停止PostgreSQL数据库


-bash-3.2$ pg_ctl stop -D /var/lib/pgsql/9.4/data
waiting for server to shut down.... done
server stopped
-bash-3.2$  ps -ef|grep post|grep -v grep
root      6036   499  0 22:07 pts/0    00:00:00 su - postgres
postgres  6037  6036  0 22:07 pts/0    00:00:00 -bash
postgres  6113  6037  0 22:08 pts/0    00:00:00 ps -ef

PostgreSQL默认监听端口

[root@web103 pgsql]# netstat -natp|grep postgres
tcp        0      0 127.0.0.1:5432              0.0.0.0:*                   LISTEN      4432/postgres




Linux下PostgreSQL 安装问题解决全过程

PostgreSQL 的官方下载地址为:

  ftp://ftp.postgresql.org/pub/v7.1.3/postgresql-7.1.3.tar.gz
  http://www.postgresql.org/

  如果下载最新的开发版本,你需要下载并安装 flex(版本号大于 2.5.4) 以及 bison (版本号大于 1.28)

  设计人员为了安全考虑,PostgreSQL 不能以 root 用户运行,所以必须建立对应的用户和组。

  # useradd postgre (自动建立 postgre 组)
        {注:在用useradd 命令添加一个用户后,此没有输入密码,系统并不会当空密码继续,而是无论什么密码都无法使用这个帐号,一定要用root通过passwd username给他设置一个密码激活此用户后才可以使用;第二是在useradd的时候,通过-p参数,即useradd [username] -p [password]即可直接分配密码并激活用户。}

  安装的过程并不复杂和其他源码版本的安装方法类似:
  解压到 /usr/local/src:
  # tar xvfz postgresql-7.1.3.tar.gz
  # cd postgresql-7.1.3
  # ./configure --prefix=/usr/local/pgsql
  # make
  # make install
  # chown -R postgre.postgre /usr/local/pgsql

  这样安装完毕后,并不是万事大吉了,还有一些收尾工作要做:
  # vi ~postgre/.bash_profile
  添加:

  PGLIB=/usr/local/pgsql/lib
  PGDATA=$HOME/data
  PATH=$PATH:/usr/local/pgsql/bin
  MANPATH=$MANPATH:/usr/local/pgsql/man
        LD_LIBRARYPATH=$LD_LIBRARYPATH:/usr/local/pgsql/lib
  export PGLIB PGDATA PATH MANPATH LD_LIBRARYPATH

        修改配置
       <一>.
        $vi /home/postgre/data/pg_hba.conf
        在文件的最底部:
        # "local" is for Unix domain socket   connections only
        local   all         all                               trust
        # IPv4 local connections:
        host    all         all         192.168.18.0/32 (*设置此处的网关)      trust
        # IPv6 local connections:
        host    all         all         ::1/128               trust
        <二>.
       $vi /home/postgre/data/postgresql.conf
       # - Connection Settings -
       # listen_addresses = 'localhost'            # what IP address(es) to listen on;
      把以上这一句之前的#删除,并赋值为'*'
      即改为: listen_addresses='*'
 
      
  以上配置完后,以 postgres 用户登录,
  # su - postgre
  建立数据库目录:
  $ mkdir data

  启动数据库引擎:

  $ initdb
  [postgre@www postgre]$ initdb
  This database system will be initialized with username "postgre".
  This user will own all the data files and must also own the server process.

  Fixing permissions on pre-existing data directory /home/postgre/data
  Creating database system directory /home/postgre/data/base
  Creating database XLOG directory /home/postgre/data/pg_xlog
  Creating template database in /home/postgre/data/base/template1
  Creating global relations in /home/postgre/data/base
  Adding template1 database to pg_database

  Creating view pg_user.
  Creating view pg_rules.
  Creating view pg_views.
  Creating view pg_tables.
  Creating view pg_indexes.
  Loading pg_description.
  Vacuuming database.

  Success. You can now start the database server using:

  /usr/local/pgsql/bin/postmaster -D /home/postgre/data
  or
  /usr/local/pgsql/bin/pg_ctl -D /home/postgre/data start

       以后台方式启动postgre数据库服务
  $ postmaster -i -D ~/data &
  [1] 22603
  [postgre@www postgre]$ DEBUG: Data Base System is starting up at Thu Jan 31 02:00:44 2002
  DEBUG: Data Base System was shut down at Thu Jan 31 01:57:58 2002
  DEBUG: Data Base System is in production state at Thu Jan 31 02:00:44 2002

  这样 PostgreSQL 使用位于 /usr/local/pgsql/data 的数据库,允许 Internet 用户的连接( -i ) ,并在后台运行。

      创建一个数据库
看看你能否访问数据库服务器的第一个例子就是试着创建一个数据库。 一台运行着的 PostgreSQL 服务器可以管理许多数据库。 通常我们会为每个项目和每个用户单独使用一个数据库。

你的节点管理员可能已经为你创建了可以使用的数据库。 他应该已经告诉你这个数据库的名字。如果这样你就可以省略这一步, 并且跳到下一节。

要创建一个新的数据库,在我们这个例子里叫 mydb,你可以使用下面的命令:

$ createdb mydb它应该生成下面这样的响应:
{注:若出现bash: createdb: command not found,此时可能是你未将pgsql
         的bin加到PATH环境变量中,此时在shell下执行如
          $export PATH=$PATH:/usr/local/pgsql/bin即可,同理以后若命令不存
      在的时候也可以使用此方法试一试,看一下bin的目录是否加到PATH.
}

CREATE DATABASE如果这样,那么这一步就成功了,你就可以忽略本节余下的部分了。

如果你看到类似下面这样的信息

createdb: command not found那么就是PostgreSQL没有安装好。要么是就根本没装上, 要么是你的搜索路径没有设置正确。尝试用绝对路径调用该命令试试:

$ /usr/local/pgsql/bin/createdb mydb在你的节点上这个路径可能不一样。和你的管理员联系或者看看安装指导获取正确的位置。

另外一种响应可能是这样:

createdb: could not connect to database postgres: could not connect to server:
No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/tmp/.s.PGSQL.5432"?这意味着该服务器没有启动,或者没有在 createdb 预期的地方启动。同样, 你也要检查安装指导或者找管理员。

另外一个响应可能是这样:

createdb: could not connect to database postgres: FATAL:  user "joe" does not
exist在这里提到了你自己的登陆名。如果管理员没有为你创建 PostgreSQL 用户帐号, 就会发生这些现象。(PostgreSQL 用户帐号和操作系统用户帐号是不同的。) 如果你是管理员,参阅 Chapter 18 获取创建用户帐号的帮助。 你需要变成安装 PostgreSQL 的操作系统用户的身份(通常是 postgres)才能创建第一个用户帐号。 也有可能是赋予你的 PostgreSQL 用户名和你的操作系统用户名不同; 这种情况下,你需要使用 -U 开关或者使用 PGUSER 环境变量声明你的 PostgreSQL 用户名。

如果你有个数据库用户帐号,但是没有创建数据库所需要的权限,那么你会看到下面的东西:

createdb: database creation failed: ERROR:  permission denied to create database并非所有用户都经过了创建新数据库的授权。 如果 PostgreSQL 拒绝为你创建数据库, 那么你需要让节点管理员赋予你创建数据库的权限。出现这种情况时请咨询你的节点管理员。 如果你自己安装了 PostgreSQL, 那么你应该以你启动数据库服务器的用户身份登陆然后参考手册完成权限的赋予工作。 [1]

你还可以用其它名字创建数据库。 PostgreSQL 允许你在一个节点上创建任意数量的数据库。 数据库名必须是以字母开头并且小于 63 个字符长。 一个方便的做法是创建和你当前用户名同名的数据库。 许多工具假设该数据库名为缺省数据库名,所以这样可以节省你的敲键。 要创建这样的数据库,只需要键入

$ createdb
如果你再也不想使用你的数据库了,那么你可以删除它。 比如,如果你是数据库 mydb 的所有人(创建人), 那么你就可以用下面的命令删除它:

$ dropdb mydb(对于这条命令而言,数据库名不是缺省的用户名。这样你就必须声明它。) 这个动作物理上将所有与该数据库相关的文件都删除并且不可取消, 因此做这件事之前一定要想清楚。

Notes
[1] 为什么这么干就行了? 解释:PostgreSQL 用户名是和操作系统用户账号分开的。 如果你与一个数据库联接,你可以选择以何种 PostgreSQL 用户名进行联接; 如果你不选择,那么缺省就是你的当前操作系统账号。 如果这样,那么总有一个与操作系统用户同名的 PostgreSQL 用户账号用于启动服务器, 并且通常这个用户都有创建数据库的权限。如果你不想以该用户身份登陆, 那么你也可以在任何地方声明一个 -U 选项以选择一个联接的 PostgreSQL 用户名。

访问数据库
一旦你创建了数据库,你就可以访问它:



运行 PostgreSQL 交互的终端程序, 叫 psql, 它允许你交互地输入,编辑,和执行 SQL 命令。

使用我们现有的图形前端工具,比如 PgAccess 或者带 ODBC 支持的办公套件来创建和管理数据库。 这种方法在这份教程中没有介绍。

写一个客户应用,使用多种语言绑定中的一种。 这些可能性在 Part IV 中有更深入的讨论。

你可能需要启动 psql,试验本教程中的例子。 你可以用下面的命令为 mydb 数据库激活它:

$ psql mydb如果你省略了数据库名字,那么它缺省就是你的用户账号名字。 你已经在前面的小节里知道这个大纲了。

在 psql 里,你会看到下面的欢迎信息:

Welcome to psql 8.1, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

mydb=>最后一行也可能是

mydb=#这个提示符意味着你是数据库超级用户,最可能出现在你自己安装了 PostgreSQL 的情况下。作为超级用户意味着你不受访问控制的限制。 对于本教程的目的而言,是否超级用户并不重要。

如果你启动 psql 时碰到了问题,那么回到前面的小节。 诊断 createdb 的方法和诊断 psql 的方法很类似, 如果前者能运行那么后者也应该能运行。

psql 打印出的最后一行是提示符,它表示 psql 正听着你说话,这个时候你就可以敲入 SQL 查询到一个 psql 维护的工作区中。试验一下下面的命令:

mydb=> SELECT version();
                            version
----------------------------------------------------------------
PostgreSQL 8.1 on i586-pc-linux-gnu, compiled by GCC 2.96
(1 row)
mydb=> SELECT current_date;
    date
------------
2002-08-31
(1 row)

mydb=> SELECT 2 + 2;
?column?
----------
        4
(1 row)
psql 程序有一些不属于 SQL 命令的内部命令。 它们以反斜杠开头,"\"。 有些这种命令在欢迎信息中列出。比如,你可以用下面的命令获取各种PostgreSQL SQL 命令的帮助语法:

mydb=> \h
要退出 psql,键入

mydb=> \q然后 psql 就会退出并且给你返回到命令行 shell。 (要获取更多有关内部命令的信息,你可以在 psql 提示符上键入 \?。) psql 的完整功能在 Part VI 中有文档。如果 PostgreSQL 安装正确,那么你还可以在操作系统的 shell 提示符上键入 man psql 来阅读该文档。在这份文档里,我们将不会明确使用这些特性,但是你自己可以在合适的时候使用它们。

本文来源:http://www.bbyears.com/shujuku/79854.html

热门标签

更多>>

本类排行