Ubuntu安装、使用postgresql数据库


Ubuntu安装、使用postgresql数据库mysql


$ sudo apt-get install postgresql  (端口为5432)sql


$ sudo apt-get install postgresql-contrib   (这个主要是为了生成oid2name)数据库


$ sudo passwd postgres   (postgres帐号的家目录为:/var/lib/postgresql)ide


安装postgres图形化客户端post


$ sudo apt-get install pgadmin3    (调用直接在命令行里输入pgadmin3便可)spa



postgres的主要配置文件命令行


/etc/postgresql/9.1/main/下的pg_hba.conf(链接数据库的身份验证方式)和postgresql.conf(数据库的rest


配置文件)  (建议修改前都备份下)postgresql




postgresql的bin命令路径:orm


/usr/lib/postgresql/9.1/bin  


默认不写绝对路径没法调用,可作下软连接:


# ln -s /usr/lib/postgresql/9.1/bin/* /usr/bin/   (有些原本就有,无所谓的)



# mkdir -p /home/postgresql/data   (也可不调,默认路径为:/var/lib/postgresql/9.1/main/base)


# chown postgres:postgres /home/postgresql/data


# su postgres


$ initdb -D /home/postgresql/data



数据库存放路径:/home/postgresql/data/base  (这里显示的是数据库名的OID)


如要查看OID对应的数据库名,在base目录下执行oid2name便可




开启postgresql  log


# vi /home/postgresql/data/postgres.conf (log部分默认均为注释)


按a或i进入编辑模式


log_destination = 'stderr'


logging_collector = on


log_directory = 'pg_log'   (绝对路径为:/var/lib/postgresql/9.1/main/)


log_filename = 'postgresql-%y-%m-%d_%H%M%S.log'


log_rotation_size = 10MB


按Esc键退出编辑模式


:wq  (保存并退出)




容许从其它主机进行登陆:


# vi /home/postgresql/data/pg_hba.conf


按a或i进入编辑模式


host  all all   10.0.0.3/32    md5  (md5为须要密码,trust为不须要密码)


按Esc键退出编辑模式


:wq  (保存并退出)



其它主机登陆:


# psql -h 10.0.0.2 -p 5432 mingdb ming





启动、关闭postgresql


# service postgresql restart    (-D的方法启动的话就只能杀进程了)




$ postgres -D /home/postgresql/data (关闭可用kill)



$ pg_ctl -D /home/postgresql/data start   (关闭是stop)



注:启动后如需作其余操做建议另外打开个窗口(如ctrl+z的话会形成psql没法登录)



在postgres帐号命令行下操做步骤以下:


建立帐号


$ createuser -A -D -P -R user1


密码


建立帐号方法2:


$ createuser user1 with password '****'



修改帐号密码命令:


$ alter user1 password 'xxxx'



删除帐号:


$ dropuser user1


注:


-A 不容许建立其余用户

-D 不容许建立数据库

-P 建立密码

-R 不容许建立角色



建立数据库ming


$ createdb ming


删除数据库ming


$ dropdb ming


进入数据库ming


$ psql ming


进入后界面以下


ming=#




在数据库下操做步骤以下:



$ psql -U postgres  (在postgres帐号下可直接psql)



为postgres帐号设置密码(注意区分系统帐号postgres和数据库里postgres帐号):


postgres=# \password postgres  


密码



建立帐号



postgres=# create user "ming" with password '123456' nocreatedb;


创建数据库并指定owner


postgres=# create database "mingdb" with owner="ming";




\l:列出已有数据库的Owner,Encoding等信息


\c  数据库名:切换数据库


\d:查看当前数据库中已有的表


\d 表名:查看表结构


\du:列出全部用户


\q:退出


查看数据表内容(同mysql):


select * from aa;

相关文章
相关标签/搜索