POSTGRESQL数据库在Windows下以管理员用户安装和简单命令
最近因为用HYPERIC产品,装了一下Postgres数据库,下面简说下在WINDOWS下安装的情况。
下载那个直接解压版,解压
在"$PG"目录下创建一个rootpass.txt文件,内容为数据库的超级用户密码。可以填个“p”,方便后面登陆。
准备工作到此结束,下面的步骤以管理员身份执行。
移动DLL文件[ 8.1.5 及以上版本不需要这一步骤]:
cd $PG
move /y lib\comerr32.dll bin\
move /y lib\krb5_32.dll bin\
move /y lib\libeay32.dll bin\
move /y lib\libiconv-2.dll bin\
move /y lib\libintl-2.dll bin\
move /y lib\libpq.dll bin\
move /y lib\pthreadGC2.dll bin\
move /y lib\ssleay32.dll bin\
添加新的postgres用户,并将密码设为:p
net user postgres p /ADD /EXPIRES:NEVER /PASSWORDCHG:NO
net localgroup users postgres /delete
创建data目录并设置访问权限:
md data
cacls . /T /E /P postgres:R
cacls data /T /E /P postgres:C
POSTGRES不支持管理员状态运行,我们用RUNAS来做,如果失败,可以查下是不是有个SECOND LOGON服务是不是停了,启动一下即可。
初始化PostgreSQL数据库,切换用户时需要手动输入postgres用户的密码:p
runas /noprofile /env /user:postgres "bin\initdb -D data -E EUC_CN --locale=\"Chinese_People's Republic of China.936\ " -A md5 -U postgres --pwfile=rootpass.txt"
这样就安装好了。需要说明的是数据库默认编码为:EUC_CN(GB2312),区域设置为:zh_CN.GBK,数据库超级用户名为:root,密码为rootpass.txt文件内容,使用md5认证。
以后可以使用:
runas /noprofile /env /user:postgres "bin\pg_ctl start -w -D data"
启动PG,使用:
runas /noprofile /env /user:postgres "bin\pg_ctl stop -D data -m smart"
关闭PG。
用登录:
runas /noprofile /env /user:postgres "bin\psql -U postgres"
登录后就可以创用户,创数据库。
一些命令对比:
PostgreSQL与MySQL命令比较 |
PostgreSQL |
MySQL |
服务启动: 1)#service postgresql start 2)#/etc/init.d/postgresql start 3)#su – postgresql $pg_ctl start PostgreSQL的进程号:1210、1207、 |
服务启动: 1)#service mysqld start 2)#/etc/init.d/mysqld start 3)#safe_mysqld& MySQL的进程号为1663 |
第一次进入数据库: #su – postgres $createdb (建名为postgres的数据库) $psql |
第一次进入数据库: #mysql mysql> (出现这个提示符说明成功) |
创建用户:(用户Ajian,密码:123) #su – postgres $psql =#create user ajian with password ‘ 123’
|
创建用户:(用户Ajian,密码:123) #grant all privileges on *.* to ajian@"%" identified by "123" (注意:同还可以分配权限,这里是ALL) |
创建数据库(My): #su – postgres $psql =#create database My with owner = ajian template = template1 encoding=’UNICODE’; |
创建数据库(My): 1)#mysql Mysql>create database My; 2)#mysqladmin create My |
查看用户和数据库: #su – postgres $psql =#\l (查看数据库) =#\du (查看用户) |
查看用户和数据库: 1)#mysql Mysql>show databases; (看数据库) 2)#mysqlshow |
新建用户登录: (首先修改配置文件) # vi /var/lib/pgsql/data/pg_hba.conf(在最后加) host all all 127.0.0.1 255.255.255.255 md5 再重启服务:#service postgresql restart 登录:#psql –h 127.0.0.1 –U ajian My Password: |
新建用户登录: 1)#mysql –u ajian –p (带口令登录) 2)#mysql Mysql>use My; (不带口令登录一般用于本机) |
创建表(employee): =#create table employee( (#employee_id int primary key, (#name char(8), (#sex char(2)); |
创建表: >create table employee( ->employee_id int primary key, ->name char(8), ->sex char(2)); |
查看表: =#\dt |
查看表: >show tables; |
查看表的结构: =#\d employee |
查看表的结构: >sescribe employee; |
向表中添加数据: =#insert into employee values -#(‘ 1’ ,’zhang’,’F’); -#(‘ 2’ ,’chen’,’M’,); |
向表中添加数据: >insert into employee values ->(‘ 1’ ,’zhang’,’F’); ->(‘ 2’ ,’chen’,’M’,); |
查看表的数据: =#select * from emlpoyee |
查看表的数据: >select * from emlpoyee; |
创建索引(IN_employee): =#create index IN_employee on employee(name); 查看索引: =#\di 删除索引: =#drop index IN_employee on employee; 重建索引: =#reindex table employee;(重建employee所有的) =#reindex index IN_employee;(重建指定的) |
创建索引(IN_employee): 1)>create index IN_employee on employee(name); 2)>alter table employee add index IN_employee(name); 查看索引: >show index from employee; 删除索引: 1)>drop index IN_employee on employee; 2)>alter table emlpoyee drop index IN_employee; |
删除表: =#drop table employee; |
删除表: >drop table employee; |
删除数据库:(注意命令前面的标志) 1)=#drop database ajian; 2)$dropdb ajian |
删除数据库:(注意命令前面的标志) 1)>drop database ajian; 2)#mysqladmin drop ajian |
|
|
有兴趣可以访问下我的生活博客: qqmovie.qzone.com