Postgresql服务部署
PostgreSQL 是一种非常复杂的对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大,特性最丰富和最复杂的自由软件数据库系统。 ip:192.168.85.130 hostname: vm2.lansgg.com pg 版本:postgresql-9.2.4.tar.bz2 一、yum安装 三、系统数据库 1、yum安装 [root@vm2~]#wget [root@vm2~]#rpm-vhipgdg-redhat92-9.2-8.noarch.rpm [root@vm2~]#yuminstallpostgresql92-serverpostgresql92-contrib-y 1.2、初始化并启动数据库 [root@vm2~]#/etc/init.d/postgresql-9.2initdb 正在初始化数据库:[确定] [root@vm2~]#/etc/init.d/postgresql-9.2start 启动postgresql-9.2服务:[确定] [root@vm2~]#echo"PATH=/usr/pgsql-9.2/bin:$PATH">>/etc/profile [root@vm2~]#echo"exportPATH">>/etc/profile 1.3、测试 [root@vm2~]#su-postgres -bash-4.1$psql psql(9.2.19) 输入"help"来获取帮助信息. postgres=#l 资料库列表 名称|拥有者|字元编码|校对规则|Ctype|存取权限 -----------+----------+----------+-------------+-------------+----------------------- postgres|postgres|UTF8|zh_CN.UTF-8|zh_CN.UTF-8| template0|postgres|UTF8|zh_CN.UTF-8|zh_CN.UTF-8|=c/postgres+ |||||postgres=CTc/postgres template1|postgres|UTF8|zh_CN.UTF-8|zh_CN.UTF-8|=c/postgres+ |||||postgres=CTc/postgres (3行记录) postgres=# 1.4、修改管理员密码 修改PostgreSQL 数据库用户postgres的密码(注意不是linux系统帐号) postgres=#select*frompg_shadow; usename|usesysid|usecreatedb|usesuper|usecatupd|userepl|passwd|valuntil|useconfig ----------+----------+-------------+----------+-----------+---------+--------+----------+----------- postgres|10|t|t|t|t||| (1行记录) postgres=#ALTERUSERpostgresWITHPASSWORD'postgres'; ALTERROLE postgres=#select*frompg_shadow; usename|usesysid|usecreatedb|usesuper|usecatupd|userepl|passwd|valuntil|useconfig ----------+----------+-------------+----------+-----------+---------+-------------------------------------+----------+----------- postgres|10|t|t|t|t|md53175bce1d3201d16594cebf9d7eb3f9d|| (1行记录) postgres=# 1.5、创建测试数据库 postgres=#createdatabasetestdb; CREATEDATABASE postgres=#ctestdb; 您现在已经连线到数据库"testdb",用户"postgres". testdb=# 1.6、创建测试表 testdb=#createtabletest(idinteger,nametext); CREATETABLE testdb=#insertintotestvalues(1,'lansgg'); INSERT01 testdb=#select*fromtest; id|name ----+-------- 1|lansgg (1行记录) 1.7、查看表结构 testdb=#dtest; 资料表"public.test" 栏位|型别|修饰词 ------+---------+-------- id|integer| name|text| 1.8、修改PostgresSQL 数据库配置实现远程访问 修改postgresql.conf 文件 如果想让PostgreSQL 监听整个网络的话,将listen_addresses 前的#去掉,并将 listen_addresses = 'localhost' 改成 listen_addresses = '*' [root@vm2~]#vim/var/lib/pgsql/9.2/data/pg_hba.conf hostallall127.0.0.1/32ident hostallallallmd5 [root@vm2~]#/etc/init.d/postgresql-9.2restart 停止postgresql-9.2服务:[确定] 启动postgresql-9.2服务:[确定] [root@vm2~]# 2、源码安装 停止上面yum安装的pgsql服务,下载PostgreSQL 源码包 [root@vm2~]#/etc/init.d/postgresql-9.2stop 停止postgresql-9.2服务:[确定] [root@vm2~]#wget [root@vm2~]#tarjxvfpostgresql-9.2.4.tar.bz2 [root@vm2~]#cdpostgresql-9.2.4 查看INSTALL 文件 [root@vm2postgresql-9.2.4]#./configure [root@vm2postgresql-9.2.4]#gmake [root@vm2postgresql-9.2.4]#gmakeinstall [root@vm2postgresql-9.2.4]#echo"PGHOME=/usr/local/pgsql">>/etc/profile [root@vm2postgresql-9.2.4]#echo"exportPGHOME">>/etc/profile [root@vm2postgresql-9.2.4]#echo"PGDATA=/usr/local/pgsql/data">>/etc/profile [root@vm2postgresql-9.2.4]#echo"exportPGDATA">>/etc/profile [root@vm2postgresql-9.2.4]#echo"PATH=$PGHOME/bin:$PATH">>/etc/profile [root@vm2postgresql-9.2.4]#echo"exportPATH">>/etc/profile [root@vm2postgresql-9.2.4]#source/etc/profile [root@vm2postgresql-9.2.4]# 2.2、初始化数据库 useradd-d/opt/postgrespostgres######如果没有此账户就创建,前面yum安装的时候已经替我们创建了 [root@vm2postgresql-9.2.4]#mkdir/usr/local/pgsql/data [root@vm2postgresql-9.2.4]#chownpostgres.postgres/usr/local/pgsql/data/ [root@vm2postgresql-9.2.4]#su-postgres -bash-4.1$/usr/local/pgsql/bin/initdb-D/usr/local/pgsql/data/ Thefilesbelongingtothisdatabasesystemwillbeownedbyuser"postgres". Thisusermustalsoowntheserverprocess. Thedatabaseclusterwillbeinitializedwithlocale"zh_CN.UTF-8". Thedefaultdatabaseencodinghasaccordinglybeensetto"UTF8". initdb:couldnotfindsuitabletextsearchconfigurationforlocale"zh_CN.UTF-8" Thedefaulttextsearchconfigurationwillbesetto"simple". fixingpermissionsonexistingdirectory/usr/local/pgsql/data...ok creatingsubdirectories...ok selectingdefaultmax_connections...100 selectingdefaultshared_buffers...32MB creatingconfigurationfiles...ok creatingtemplate1databasein/usr/local/pgsql/data/base/1...ok initializingpg_authid...ok initializingdependencies...ok creatingsystemviews...ok loadingsystemobjects'descriptions...ok creatingcollations...ok creatingconversions...ok creatingdictionaries...ok settingprivilegesonbuilt-inobjects...ok creatinginformationschema...ok loadingPL/pgSQLserver-sidelanguage...ok vacuumingdatabasetemplate1...ok copyingtemplate1totemplate0...ok copyingtemplate1topostgres...ok WARNING:enabling"trust"authenticationforlocalconnections Youcanchangethisbyeditingpg_hba.conforusingtheoption-A,or --auth-localand--auth-host,thenexttimeyouruninitdb. Success.Youcannowstartthedatabaseserverusing: /usr/local/pgsql/bin/postgres-D/usr/local/pgsql/data or /usr/local/pgsql/bin/pg_ctl-D/usr/local/pgsql/data-llogfilestart -bash-4.1$ 1.3、添加到系统服务 -bash-4.1$exit logout [root@vm2postgresql-9.2.4]#cp/root/postgresql-9.2.4/contrib/start-scripts/linux/etc/init.d/postgresql [root@vm2postgresql-9.2.4]#chmod+x/etc/init.d/postgresql [root@vm2postgresql-9.2.4]#/etc/init.d/postgresqlstart StartingPostgreSQL:ok [root@vm2postgresql-9.2.4]#chkconfig--addpostgresql [root@vm2postgresql-9.2.4]#chkconfigpostgresqlon [root@vm2postgresql-9.2.4]# 1.4、测试使用 [root@vm2postgresql-9.2.4]#su-postgres -bash-4.1$psql-l Listofdatabases Name|Owner|Encoding|Collate|Ctype|Accessprivileges -----------+----------+----------+-------------+-------------+----------------------- postgres|postgres|UTF8|zh_CN.UTF-8|zh_CN.UTF-8| template0|postgres|UTF8|zh_CN.UTF-8|zh_CN.UTF-8|=c/postgres+ |||||postgres=CTc/postgres template1|postgres|UTF8|zh_CN.UTF-8|zh_CN.UTF-8|=c/postgres+ |||||postgres=CTc/postgres (3rows) -bash-4.1$psql psql(9.2.4) Type"help"forhelp. postgres=#createdatabasetestdb; CREATEDATABASE postgres=#ctestdb; Youarenowconnectedtodatabase"testdb"asuser"postgres". testdb=#createtabletest(idint,nametext,ageint); CREATETABLE testdb=#dtest Table"public.test" Column|Type|Modifiers --------+---------+----------- id|integer| name|text| age|integer| testdb=#insertintotestvalues(1,'lansgg',25); INSERT01 testdb=#select*fromtest; id|name|age ----+--------+----- 1|lansgg|25 (1row) testdb=# 3、系统数据库 在创建数据集簇之后,该集簇中默认包含三个系统数据库template1、template0和postgres。其中template0和postgres都是在初始化过程中从template1拷贝而来的。 template1和template0数据库用于创建数据库。PostgreSQL中采用从模板数据库复制的方式来创建新的数据库,在创建数据库的命令中可以用“-T”选项来指定以哪个数据库为模板来创建新数据库。 template1数据库是创建数据库命令默认的模板,也就是说通过不带“-T”选项的命令创建的用户数据库是和template1一模一样的。template1是可以修改的,如果对template1进行了修改,那么在修改之后创建的用户数据库中也能体现出这些修改的结果。template1的存在允许用户可以制作一个自定义的模板数据库,在其中用户可以创建一些应用需要的表、数据、索引等,在日后需要多次创建相同内容的数据库时,都可以用template1作为模板生成。 由于template1的内容有可能被用户修改,因此为了满足用户创建一个“干净”数据库的需求,PostgreSQL提供了template0数据库作为最初始的备份数据,当需要时可以用template0作为模板生成“干净”的数据库。 而第三个初始数据库postgres用于给初始用户提供一个可连接的数据库,就像Linux系统中一个用户的主目录一样。 上述系统数据库都是可以删除的,但是两个模板数据库在删除之前必须将其在pg_database中元组的datistemplate属性改为FALSE,否则删除时会提示“不能删除一个模板数据库” (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |