cdh PostgreSQL

来源:互联网 发布:细说php第二版 编辑:程序博客网 时间:2024/06/08 09:25

centos用yum安装postgresql的路径为 /var/lib/pgsql/中。

1.安装postgresql9.0 yum 仓库

rpm -i http://yum.postgresql.org/9.2/redhat/rhel-6-x86_64/pgdg-redhat92-9.2-7.noarch.rpm

2.安装新版本的Postgresql

yum install postgresql92-server postgresql92-contrib

3.初始化数据库

/etc/init.d/postgresql-9.2 initdb

4.启动数据库

/etc/init.d/postgresql-9.2 start

注意:postgresql启动后就可以利用service postgresql-9.2 start/restart/stop来控制它了。
虽然打完service后,按p不提示postgresql-9.2,但是可以用手输。

5.把postgresql加入自启动列表

cd /etc/init.d
chkconfig –add postgresql9.2

6.查看一下自启动列表

chkconfig –list

在这里可以看到postgresql已经在其中了。

7.PostgreSQL 数据库默认会创建一个postgres的数据库用户作为数据库的管理员,默认密码为空,我们需要修改为指定的密码,这里设定为’postgres’

直接在控制台输入以下命令:

# su - postgres$ psql#CREATE USER amon WITH PASSWORD 'fuzegame';#CREATE DATABASE amon OWNER amon;#GRANT ALL PRIVILEGES ON DATABASE amon to amon;#\l 显示数据库# \c amon 进入数据库#\d 查看所有表#amon =# create table test (id integer, name text);#amon =# insert into test values (1,'david');

至上,安装成功。


ps:1.可以通过查找pg_hba.conf,来定位postgresql的位置。
2.重启postgresql,可以用service postgresql restart

第一种方法,使用PostgreSQL控制台。
首先,新建一个Linux新用户,可以取你想要的名字,这里为dbuser。
sudo adduser dbuser
然后,切换到postgres用户。
sudo su - postgres
下一步,使用psql命令登录PostgreSQL控制台。
psql
这时相当于系统用户postgres以同名数据库用户的身份,登录数据库,这是不用输入密码的。如果一切正常,系统提示符会变为”postgres=#”,表示这时已经进入了数据库控制台。以下的命令都在控制台内完成。
第一件事是使用\password命令,为postgres用户设置一个密码。
\password postgres
第二件事是创建数据库用户dbuser(刚才创建的是Linux系统用户),并设置密码。
CREATE USER dbuser WITH PASSWORD ‘password’;
第三件事是创建用户数据库,这里为exampledb,并指定所有者为dbuser。
CREATE DATABASE exampledb OWNER dbuser;
第四件事是将exampledb数据库的所有权限都赋予dbuser,否则dbuser只能登录控制台,没有任何数据库操作权限。
GRANT ALL PRIVILEGES ON DATABASE exampledb to dbuser;
最后,使用\q命令退出控制台(也可以直接按ctrl+D)。
\q

问题1:
org.postgresql.util.PSQLException: Connection refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
解决办法:Edit /var/lib/pgsql/data/postgresql.conf file
Change
#listen_addresses = ‘localhost’
to
listen_addresses = ‘*’

问题2:
org.postgresql.util.PSQLException: FATAL: no pg_hba.conf entry for host “”, user “fkong”, database “fkong”, SSL off
解决办法:
Edit /var/lib/pgsql/data/pg_hba.conf file
Add below line under “# IPv4 local connections:”
“host all all /32 password”

问题3:
org.postgresql.util.PSQLException: FATAL: Ident authentication failed for user “fkong”
解决办法:
Edit /var/lib/pgsql/data/pg_hba.conf file
Change
“host all all /32 ident”
to
“host all all /32 password”

0 0