fedora15安装使用postgresql



fedora15安装使用postgresql

注:安装完pgsql后,一定要安装推荐的插件,否者初始化数据库是不会成功。

这个插件就是PostGIS 1.5

一.postgresql安装合适用

1.二进制文件安装PostgreSQL8.4
1.1二进制文件下载
1)下载地址:http://www.postgresql.org/download/
2)PostgreSQL是开源数据库,可以通过官方地址下载,选择Binary packages –>Linux –> Download for 64bit Linux选项
1.2安装步骤
1)创建相应的目录文件,存放安装文件 postgresql-8.4.0-1-linux.bin
2)通过ROOT权限用户运行安装文件,最好是ROOT用户
   # ./postgresql-8.4.0-1-linux.bin
3)指定postgresql-8.4的安装目录,例如:/app/postgres/8.4
4)指定postgresql-8.4的DATA目录,例如:/app/postgres/8.4/data
5)指定超级用户postgres的登录密码
6)指定postgresql运行端口,默认port:5432
7)指定postgresql 的locale,通常为:C
8)进入安装过程
9)配置环境变量
  # vi /etc/profile
  export PATH=/app/postgres/8.4/bin:$PATH
  export  PGDATA=/app/postgres/8.4/data
10)配置远程连接:
    编辑  /app/postgres/8.4/data/pg_hba.conf
    添加  host all all ip/24   trust
11)启动数据库PostgreSQL
    # su – postgres
    $ ./initdb –D  $PGDATA
    $ pg_ctl start

2.源文件安装PostgreSQL8.4

2.1源文件下载
1)下载地址:http://www.postgresql.org/download/
2)PostgreSQL是开源数据库,可以通过官方地址下载,选择Source code –>Source code –>v8.4 –>PostgreSQL-8.4.3.tar.gz,选择镜像下载

2.2安装步骤
1)root权限用户或root用户下解压下载文件
  # tar zxvf postgresql-8.4.3.tar.gz
  # cd postgresql-8.4.3
2)进行配置,指定PostgreSQL的安装目录
  # ./configure –prefix=/app/postgres/8.4
3)进行编译
  # make
4)进入安装
  # make install
5)创建用户组和用户,目录
  # groupadd postgres
  # useradd –g postgres –d /app/postgres postgres
  # mkdir /app/postgres/8.4/data
  # chown -R postgres.postgres  /app/postgres/8.4
6)配置环境变量
  # vi /etc/profile
    export  PATH=/app/postgres/8.4/bin:$PATH
    export  PGDATA=/app/postgres/8.4/data/
7)配置数据库监听地址和端口
  编辑 /app/postgres/8.4/data/postgresql.conf
  生效如下2行:
  listen_addresses = ‘*’
  port = 5432
8)配置远程连接
  编辑  /app/postgres/8.4/data/pg_hba.conf
  添加   host all all ip/24  trust
9)初始化数据库目录:
 # su – postgres
 $ ./initdb –D $PGDATA
10)启动数据库PostgreSQL
  # su – postgres
  $ ./postmaster –D $PGDATA

 

1.安装

yum install postgresql-server postgresql
安装完成后还有一个必须的步骤:初始化数据目录。由于postgresql是以postgres用户运行的,目录的属主必须指定为postgres。默认的数据目录是/var/lib/pgsql/data,如果不是则需要用-D参数指定。

<!–没有测试–>

首先需要切换为postgres用户才能运行initdb命令,另外要注意当前系统的locale,windows下ssh登录的zh_CN.GB18030就安装不了,把语言设置为en_US.UTF8就行了。
su postgres
initdb -D /var/lib/pgsql/data

如果指定的不是默认数据目录,需要更改/etc/init.d/postgresql启动脚本,把PGDATA,PGLOG目录指定新设置的目录。如果启动有问题,查看数据目录下的pgstartup.log日志文件
chkconfig postgresql on
service postgresql start

配置文件是数据目录下的postgresql.conf, 默认监听5432端口,所有IP地址。
listen_addresses=’192.168.1.11′ 改为只监听内网IP

netstat -nltp 看看有没有5432端口在监听
pgsql -U postgres 以postgres用户身份连接postgresql

<!–没有测试over–>

2.启动Postgresql
  1.     1,如果你的Postgresql已经运行了,跳到第5步.
  2.     2,以root身份,在命令行中,输入service postgresql initdb.(根据路径的设置情况,你可能需要在命令行中使用/sbin/service).完成以后,你应该得到一个一切正常的反馈.
  3.     3,仍以root身份,在命令行中,输入service postgresql start.等待,直到命令执行完成.
  4.     4,为了让postgresql开机就启动, 以root身份,在命令行中,输入chkconfig –level 345 postgresql on.也有图形界面的方式,用于启动/停止服务,但是我喜欢使用命令行.

3.从当前用户切换到postgres用户

 例如我当前用户是sinor,那输入 su -,切换到人root用户。再输入su postgres切换到postgres用户。

4.创建postgres用户


在postgres用户下输入以下指令:

例如创建名为ru的用户:
createuser -r -d -s ru

createuser [options] 用户名
【可以通过createuser –help命令得到帮助信息】

   Usage:
 createuser [OPTION]… [ROLENAME]

Options:
 -c, –connection-limit=N  connection limit for role (default: no limit)
 -d, –createdb            role can create new databases
 -D, –no-createdb         role cannot create databases
 -e, –echo                show the commands being sent to the server
 -E, –encrypted           encrypt stored password
 -i, –inherit             role inherits privileges of roles it is a
                           member of (default)
 -I, –no-inherit          role does not inherit privileges
 -l, –login               role can login (default)
 -L, –no-login            role cannot login
 -N, –unencrypted         do not encrypt stored password
 -P, –pwprompt            assign a password to new role
 -r, –createrole          role can create new roles
 -R, –no-createrole       role cannot create roles
 -s, –superuser           role will be superuser
 -S, –no-superuser        role will not be superuser
 –help                    show this help, then exit
 –version                 output version information, then exit

Connection options:
 -h, –host=HOSTNAME       database server host or socket directory
 -p, –port=PORT           database server port
 -U, –username=USERNAME   user name to connect as (not the one to create)
 -w, –no-password         never prompt for password
 -W, –password            force password prompt

If one of -d, -D, -r, -R, -s, -S, and ROLENAME is not specified, you will
be prompted interactively.

5.创建数据库
  例如创建名为mydb的数据库
   createdb  mydb
  createdb [options] 数据库名
 
Usage:
 createdb [OPTION]… [DBNAME] [DESCRIPTION]

Options:
 -D, –tablespace=TABLESPACE  default tablespace for the database
 -e, –echo                   show the commands being sent to the server
 -E, –encoding=ENCODING      encoding for the database
 -l, –locale=LOCALE          locale settings for the database
     –lc-collate=LOCALE      LC_COLLATE setting for the database
     –lc-ctype=LOCALE        LC_CTYPE setting for the database
 -O, –owner=OWNER            database user to own the new database
 -T, –template=TEMPLATE      template database to copy
 –help                       show this help, then exit
 –version                    output version information, then exit

Connection options:
 -h, –host=HOSTNAME          database server host or socket directory
 -p, –port=PORT              database server port
 -U, –username=USERNAME      user name to connect as
 -w, –no-password            never prompt for password
 -W, –password               force password prompt

By default, a database with the same name as the current user is created.
6.删除数据库
  删除mydb数据库:
  dropdb mydb
 
  Usage:
 dropdb [OPTION]… DBNAME

Options:
 -e, –echo                show the commands being sent to the server
 -i, –interactive         prompt before deleting anything
 –help                    show this help, then exit
 –version                 output version information, then exit

Connection options:
 -h, –host=HOSTNAME       database server host or socket directory
 -p, –port=PORT           database server port
 -U, –username=USERNAME   user name to connect as
 -w, –no-password         never prompt for password
 -W, –password            force password prompt

7.连接数据库
例如我的数据库是123
psql  123
Usage:
 psql [OPTION]… [DBNAME [USERNAME]]

General options:
 -c, –command=COMMAND    run only single command (SQL or internal) and exit
 -d, –dbname=DBNAME      database name to connect to (default: “postgres”)
 -f, –file=FILENAME      execute commands from file, then exit
 -l, –list               list available databases, then exit
 -v, –set=, –variable=NAME=VALUE
                          set psql variable NAME to VALUE
 -X, –no-psqlrc          do not read startup file (~/.psqlrc)
 -1 (“one”), –single-transaction
                          execute command file as a single transaction
 –help                   show this help, then exit
 –version                output version information, then exit

Input and output options:
 -a, –echo-all           echo all input from script
 -e, –echo-queries       echo commands sent to server
 -E, –echo-hidden        display queries that internal commands generate
 -L, –log-file=FILENAME  send session log to file
 -n, –no-readline        disable enhanced command line editing (readline)
 -o, –output=FILENAME    send query results to file (or |pipe)
 -q, –quiet              run quietly (no messages, only query output)
 -s, –single-step        single-step mode (confirm each query)
 -S, –single-line        single-line mode (end of line terminates SQL command)

Output format options:
 -A, –no-align           unaligned table output mode
 -F, –field-separator=STRING
                          set field separator (default: “|”)
 -H, –html               HTML table output mode
 -P, –pset=VAR[=ARG]     set printing option VAR to ARG (see \pset command)
 -R, –record-separator=STRING
                          set record separator (default: newline)
 -t, –tuples-only        print rows only
 -T, –table-attr=TEXT    set HTML table tag attributes (e.g., width, border)
 -x, –expanded           turn on expanded table output

Connection options:
 -h, –host=HOSTNAME      database server host or socket directory (default: “local socket”)
 -p, –port=PORT          database server port (default: “5432″)
 -U, –username=USERNAME  database user name (default: “postgres”)
 -w, –no-password        never prompt for password
 -W, –password           force password prompt (should happen automatically)

For more information, type “\?” (for internal commands) or “\help” (for SQL
commands) from within psql, or consult the psql section in the PostgreSQL
documentation.

mydb=# 注:最后一行 mydb=#,这个提示符意味着您是数据库超级用户

二.Postgresql图形化管理工具pgAdmin3
  
以fedora为例
 yum install pgadmin3
 完成安装后在   应用程序->编程->pgAdmin3

http://blog.sina.com.cn/s/blog_976e49570100yn8r.html