① 到 http://www.postgresql.org/ 下载 PostgreSQL for Win 的 no installer 版本。现在最新的版本是8.2.5-1。直接解压就可以了。例如:
C:\postgreSQL
② 设置环境变量
C:> set PGHOME=C:\postgreSQL
C:> set PGDATA=%PGHOME%\data
C:> set PGLIB=%PGHOME%\lib
C:> set PGHOST=localhost
C:> set PATH=%PGHOME%\bin;%PATH%
③ initdb 初始化 数据库。
④ pg_ctl start 启动数据库
⑤ pg_ctl stop 停止数据库
⑥ post_svc -install 建立windows nt 服务(需要设置系统环境变量)
⑦ post_svc -remove 删除 windows nt 服务
⑧ 使用"createdb 库名" 新建数据库
⑨ 使用"createuser 用户名"创建数据库帐号
⑩ 使用"psql -h localhost -d 库名- U 用户名"进入数据库
提示:
对于入门者可以使用GUI管理工具 pgAdmin。
另附:
今天在整理资料的时候发现,Postgre出现了解压缩直接使用的版本,上边注明了for expert only. 呵呵,看来,不推荐生手使用。
为了试验一把,索性从它的官网上直接下载了一份postgresql-8.3.3-1-binaries-no-installer.zip
1. 将其解压到d:\pgsql8.3.3
2. 设置环境变量,我不想直接添加,而是写到了一个batch文件里,名为pgenv.bat
set PGHOME=d:\pgsql8.3.3
set PATH=%PGHOME%\bin;%path%
set PGHOST=localhost
set PGLIB=%PGHOME%\lib
set PGDATA=%PGHOME%\data
3.
执行 pgenv.bat
然后:
D:\pgsql8.3.3\bin>initdb
属于此数据库系统的文件宿主为用户 "******".
此用户也必须为服务器进程的宿主.
数据库簇将带有 locale Chinese_People's Republic of China.936 初始化.
initdb: locale Chinese_People's Republic of China.936 requires unsupported encod
ing GBK
Encoding GBK is not allowed as a server-side encoding.
Rerun initdb with a different locale selection.
这上边的意思好像是说不直接支持cp936, GBK,晕。于是,强制执行:
D:\pgsql8.3.3\bin>initdb --locale=zh_CN
initdb: 无效的 locale 名字 "zh_CN"
initdb: 无效的 locale 名字 "zh_CN"
initdb: 无效的 locale 名字 "zh_CN"
initdb: 无效的 locale 名字 "zh_CN"
initdb: 无效的 locale 名字 "zh_CN"
initdb: 无效的 locale 名字 "zh_CN"
属于此数据库系统的文件宿主为用户 "hex".
此用户也必须为服务器进程的宿主.
数据库簇将带有 locale Chinese_People's Republic of China.936 初始化.
initdb: locale Chinese_People's Republic of China.936 requires unsupported encod
ing GBK
Encoding GBK is not allowed as a server-side encoding.
Rerun initdb with a different locale selection.
还是不行,再换成下边的:
D:\pgsql8.3.3\bin>initdb --locale=C
属于此数据库系统的文件宿主为用户 "hex".
此用户也必须为服务器进程的宿主.
数据库簇将带有 locale C 初始化.
默认的数据库编码已经相应的设置为 SQL_ASCII.
The default text search configuration will be set to "english".
创建目录 d:/pgsql8.3.3/data ... 成功
creating subdirectories ... 成功
选择默认最大联接数 (max_connections) ... 100
selecting default shared_buffers/max_fsm_pages ... 32MB/204800
创建配置文件 ... 成功
在 d:/pgsql8.3.3/data/base/1 中创建 template1 数据库 ... 成功
initializing pg_authid ... 成功
initializing dependencies ... 成功
创建系统视图 ... 成功
loading system objects' descriptions ... 成功
创建字符集转换 ... 成功
creating dictionaries ... 成功
对内建对象设置权限 ... 成功
创建信息模式 ... 成功
清理数据库 template1 ... 成功
拷贝 template1 到 template0 ... 成功
copying template1 to postgres ... 成功
警告: 为本地连接启动了 "trust" 认证.
你可以通过编辑 pg_hba.conf 更改或你下
次运行 initdb 时使用 -A 选项.
Success. You can now start the database server using:
"postgres" -D "d:/pgsql8.3.3/data"
or
"pg_ctl" -D "d:/pgsql8.3.3/data" -l logfile start
初始化后,在数据库目录里会有这样两个文件:“postgresql.conf”和“pg_hba.conf”。
修改“postgresql.conf”第30行左右的地方为顶格“tcpip_socket = true”,以允许网络访问。
“pg_hba.conf”,基于主机的访问控制(Host Based Access)。左边有“#”的内容都是被注释掉的,起说明或参考等作用。
我以我现在的文件为例做说明(里面的中文为我的说明):
[code:1:cc99a78c8e]
# PostgreSQL Client Authentication Configuration File
# ===================================================
#
# Refer to the PostgreSQL Administrator's Guide, chapter "Client
# Authentication" for a complete description. A short synopsis
# follows.
#
# This file controls: which hosts are allowed to connect, how clients
# are authenticated, which PostgreSQL user names they can use, which
# databases they can access. Records take one of three forms:
#
# local DATABASE USER METHOD [OPTION]
# host DATABASE USER IP-ADDRESS IP-MASK METHOD [OPTION]
# hostssl DATABASE USER IP-ADDRESS IP-MASK METHOD [OPTION]
#
# (The uppercase quantities should be replaced by actual values.)
# DATABASE can be "all", "sameuser", "samegroup", a database name (or
# a comma-separated list thereof), or a file name prefixed with "@".
# USER can be "all", an actual user name or a group name prefixed with
# "+" or a list containing either. IP-ADDRESS and IP-MASK specify the
# set of hosts the record matches. METHOD can be "trust", "reject",
# "md5", "crypt", "password", "krb4", "krb5", "ident", or "pam". Note
# that "password" uses clear-text passwords; "md5" is preferred for
# encrypted passwords. OPTION is the ident map or the name of the PAM
# service.
#
# This file is read on server startup and when the postmaster receives
# a SIGHUP signal. If you edit the file on a running system, you have
# to SIGHUP the postmaster for the changes to take effect, or use
# "pg_ctl reload".
# Put your actual configuration here
# ----------------------------------
#
# CAUTION: The default configuration allows any local user to connect
# using any PostgreSQL user name, including the superuser, over either
# Unix-domain sockets or TCP/IP. If you are on a multiple-user
# machine, the default configuration is probably too liberal for you.
# Change it to use something other than "trust" authentication.
#
# If you want to allow non-local connections, you need to add more
# "host" records. Also, remember TCP/IP connections are only enabled
# if you enable "tcpip_socket" in postgresql.conf.
# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
#访问来源 数据库 用户 IP-地址 子网掩码 认证方式
local typhoon typhoon md5
#对于来自本地的访问,数据库“typhoon”对用户“typhoon”采用“md5”加密口令认证
local typhoon all reject
#对于来自本地的访问,数据库“typhoon”对上面没提到的所有用户无条件拒绝认证
host typhoon typhoon 255.255.255.255 127.0.0.1 md5
#对于来网络的访问,数据库“typhoon”对用户“typhoon”,如果提出访问的是本机,采用“md5”加密口令认证
host typhoon all 0.0.0.0 0.0.0.0 reject
#对于来网络的访问,数据库“typhoon”对上面没提到的所有用户,不管提出访问的哪台机器,也不管它来自哪个子网,无条件拒绝认证
local all all md5
#对于来自本地的访问,上面没提到的数据库对上面没提到的所有用户采用“md5”加密口令认证
host all all 0.0.0.0 0.0.0.0 md5
#对于来网络的访问,数上面没提到的数据库对上面没提到的所有用户,不管提出访问的哪台机器,也不管它来自哪个子网,采用“md5”加密口令认证
########################################
# Others are all denied 其他访问一概无条件拒绝
local all all reject
host all all 0.0.0.0 0.0.0.0 teject
########################################
########################################
# All denied permissed, not safe
# 对所有访问都信任,太不安全,被我禁止掉了,只作为参考
#local all all trust
#host all all 0.0.0.0 0.0.0.0 trust
########################################
[/code:1:cc99a78c8e]
这些认证的规则是从上到下一条一条加载的,要注意的是,里面的“all”并不是真正意义上的“所有”,只是前面的规定中没有提到的那部分。个人感觉用“other”也许更恰当。
这样,最终的结果是:
所有用户都能通过加密口令访问“typhoon”以外的数据库,但是只有用户“typhoon”能以加密口令访问数据库“typhoon”。
操作篇
[客户端基本连接操作]====================
*查看PostgreSQL的系统表:
(摸索/查找中)
*终端登录:
psql -l[主机名/IP] -d[数据库名] -U[用户名]
*连接到另外一个数据库:
\c [数据库名]
*断开终端:
\q
========================================
[基本用户管理]==========================
*添加用户:
create user [用户名];
*修改用户密码:
alter user [用户名] with password [口令];
*删除用户:
drop user [用户名];
*查看用户信息:
(摸索/查找中)
========================================
[基本数据库操作]========================
*创建数据库:
create database [数据库名];
*查看数据库列表:
\d
*删除数据库:
drop database [数据库名];
*修改数据库的所有者:
(摸索/查找中)
========================================
[数据库内基本操作]======================
*给予某用户在某数据库创建表的授权:
(摸索/查找中)
*收回某用户在某数据库创建表的授权:
(摸索/查找中)
创建表:
create table ([字段名1] [类型1] <references 关联表名(关联的字段名)>,[字段名2] [类型2],......<,primary key (字段名m,字段名n,...)>);
*查看表名列表:
\d
*查看某个表的状况:
\d [表名]
*重命名一个表:
alter table [表名A] rename to [表名B];
*修改表的所有者:
(摸索/查找中)
*删除一个表:
drop table [表名];
========================================
[表内基本操作]==========================
*在已有的表里添加字段:
alter table [表名] add column [字段名] [类型];
*删除表中的字段:
alter table [表名] drop column [字段名];
*重命名一个字段:
alter table [表名] rename column [字段名A] to [字段名B];
*给一个字段设置缺省值:
alter table [表名] alter column [字段名] set default [新的默认值];
*去除缺省值:
alter table [表名] alter column [字段名] drop default;
*将已存在的表里某列设置为主码:
(摸索/查找中)
*将已存在的表里某几列设置为主码:
(摸索/查找中)
*取消某列的主码资格:
(摸索/查找中)
在表中插入数据:
insert into 表名 ([字段名m],[字段名n],......) values ([列m的值],[列n的值],......);
修改表中的某行某列的数据:
update [表名] set [目标字段名]=[目标值] where [该行特征];
删除表中某行数据:
delete from [表名] where [该行特征];
delete from [表名];--删空整个表
========================================
[备份]==================================
备份一个数据库:
pg_dump 数据库名 > 备份文件
把备份文件导回原数据库或导入新数据库:
cat 备份文件 | psql 数据库名
最好先把这个数据库的认证改为“trust”。
备份一个表:
pg_dump -t table1 dbname > bk1.sql
========================================