随笔 - 272  文章 - 0  评论 - 283  阅读 - 142万

CentOS 6.9下安装PostgreSQL

操作系统:CentOS6.9_x64

PostgreSQL官方网址: https://www.postgresql.org/

安装数据库

使用如下命令:

yum install postgresql-server -y

设置开机启动:

chkconfig postgresql on

初始化数据库

service postgresql initdb 

启动数据库:

service postgresql start

安装后,默认生成一个名为postgres的数据库和一个名为postgres的数据库用户。这里需要注意的是,同时还生成了一个名为postgres的Linux系统用户。

CentOS6.9_x64 默认安装的是8.4版本的psql,如果需要其它版本,可以参考官方文档:

https://www.postgresql.org/download/linux/redhat/

这里附上CentOS6.9_x64下的安装脚本 :

yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-6-x86_64/pgdg-centos96-9.6-3.noarch.rpm
yum install postgresql96
yum install postgresql96-server

service postgresql-9.6 initdb
chkconfig postgresql-9.6 on
service postgresql-9.6 start

配置数据库

添加新用户和新数据库

复制代码
adduser psqladmin

su - postgres

psql CREATE USER useradmin WITH PASSWORD
'123456'; CREATE DATABASE testdb OWNER useradmin; GRANT ALL PRIVILEGES ON DATABASE testdb to useradmin;
复制代码

显示用户:

\du

退出:

\q

修改 postgres 的数据库密码

\password postgres

开启远程访问:

cd /var/lib/pgsql/data/

postgresql默认情况下,远程访问不能成功,如果需要允许远程访问,需要修改两个配置文件,说明如下:

  1. postgresql.conf

将该文件中的 listen_addresses 项值设定为“*”,在9.0 Windows版中,该项配置已经是“*”无需修改。

  1. pg_hba.conf

在该配置文件的host all all 127.0.0.1/32 md5行下添加以下配置,或者直接将这一行修改为以下配置

host all all 0.0.0.0/0 md5

如果不希望允许所有IP远程访问,则可以将上述配置项中的0.0.0.0设定为特定的IP值。

使用数据库

su - postgres
psql
\c testdb

或者:

psql -U postgres -d testdb 

sql语句示例代码

使用public schema:

create table students (
    id bigserial primary key,
    name varchar(20) NOT NULL
);

insert into students values (1,'stu1');
select * from students ;
drop table students ;

使用自定义schema :

复制代码
create schema my_schema;

create table my_schema.students (
    id bigserial primary key,
    name varchar(20) NOT NULL
);

insert into my_schema.students values (1,'stu1');

select * from my_schema.students ;
复制代码

python 访问示例代码

https://github.com/mike-zhang/pyExamples/blob/master/databaseRelate/psqlOpt/psqlTest1.py

centos6.9自带的python安装pyscopg2库 : yum install python-psycopg2

好,就这些了,希望对你有帮助。

本文github地址:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2017/20170711_centos6.9下安装PostgreSQL.rst

欢迎补充

sql语句示例代码

使用public schema:

posted on   Mike_Zhang  阅读(1618)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示