快捷搜索:  汽车  科技

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)alter user postgres with password 'new password';使用 \l 查看当前的数据库列表显示数据库列表alter user 用户名 with password '新密码'; mkdir /home/postgres/pgsql/pgsql_data授权切换到rootsu给用户授权目录 # chown 用户名 文件路径chown postgres /home/postgres/pgsql su postgres首次启动数据库初始化数据库/home/postgres/pgsql/bin/initdb -D /home/postgres/pgsql/pgsql_data/初始化数据库成功示例/home/postgres/pgsql/bin/pg_ctl -D /home/postgres/pgsql/pgsql_data

介绍

PostgreSQL是以加州大学伯克利分校计算机系开发的 POSTGRES,现在已经更名为PostgreSQL,版本 4.2为基础的对象关系型数据库管理系统(ORDBMS)。PostgreSQL支持大部分 SQL标准并且提供了许多其他现代特性:复杂查询、外键、触发器、视图、事务完整性、MVCC。同样,PostgreSQL 可以用许多方法扩展,比如, 通过增加新的数据类型、函数、操作符、聚集函数、索引。免费使用、修改、和分发 PostgreSQL,不管是私用、商用、还是学术研究使用。

小编有话说

对于一个刚接触pg数据库的人来说 liunx安装PostgreSQL是痛苦的 网上的教程大多不全面 细节没有写到位,比如缺乏授权指令、用户名没有删除干净、缺乏讲述端口的开发方式等等,因此安装时经常会遇到很多问题。这篇文章就手把手教大家安装pg数据库,需要的记得收藏哦。

安装前准备
  1. CentOS 7 64 位
  2. FinalShell(或Xshell)
    1. 点击上方关注,私信:fs(或者FinalShell)获取下载链接。
  3. postgresql-10.11-1安装包
    1. 点击上方关注,私信: pg(或者postgresql)获取下载链接。
安装
  • 确认liunx是否安装了postgresql 如果有卸载
    • 列出已安装的包

rpm -qa | grep postgresql

    • 逐一删除

yum remove

    • 删除服务管理脚本

rm -f /etc/init.d/postgresql-10


  • 用户创建
    • 切换用户

彻底删除用户: userdel -rf ftpuser


su postgres

    • 若没有 创建该用户

useradd postgres passwd postgres

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)(1)

两次输入密码并确认

  • 准备安装包
    • 上传pg安装包到postgres目录下
    • 解压安装包
  • 创建pgsql数据目录: (进入: /home/postgres/pgsql)创建pgsql_data)

mkdir /home/postgres/pgsql/pgsql_data

  • 授权
    • 切换到root

su

    • 给用户授权目录 # chown 用户名 文件路径

chown postgres /home/postgres/pgsql su postgres

  • 首次启动数据库
    • 初始化数据库

/home/postgres/pgsql/bin/initdb -D /home/postgres/pgsql/pgsql_data/

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)(2)

初始化数据库成功示例

    • 启动数据库

/home/postgres/pgsql/bin/pg_ctl -D /home/postgres/pgsql/pgsql_data -l /home/postgres/pgsql/logfile start

    • 查看端口监听情况

netstat -an | grep 5432

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)(3)

查看端口

    • 命令行操作

/home/postgres/pgsql/bin/psql -h localhost -d postgres -U postgres -p 5432

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)(4)

命令行操作

    • 设置密码

alter user 用户名 with password '新密码';

alter user postgres with password 'new password';

    • 使用 \l 查看当前的数据库列表

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)(5)

显示数据库列表

    • 使用 \q 退出交互式界面

\q

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)(6)

退出命令行

    • 停止数据库

/home/postgres/pgsql/bin/pg_ctl -D /home/postgres/pgsql/pgsql_data stop

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)(7)

停止数据库成功示例

  • 设置外网可连接数据库
    • 进入pgsql数据目录 查看两个配置文件

cd /home/postgres/pgsql/pgsql_data && ls

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)(8)

文件示例

    • 在 pg_hba.conf 最后一行添加

vim pg_hba.conf

host all all 0.0.0.0/0 md5

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)(9)

添加到最后一行

保存

:wq!

    • 在 postgresql.conf 最后一行添加 listen_addresses = '*'

vim postgresql.conf

postgresql命令教学(Liunx系统安装PostgreSQL数据库教程)(10)

修改postgresql.conf文件添加 listen_addresses = '*'

    • 保存 重启gp服务即可!

/home/postgres/pgsql/bin/pg_ctl -D /home/postgres/pgsql/pgsql_data restart

firewall-cmd --add-service=postgresql --permanent 开放postgresql服务

firewall-cmd --zone=public --add-port=5432/tcp --permanent

firewall-cmd --reload 重载防火墙

//临时关闭

systemctl stop firewalld

//禁止开机启动

systemctl disable firewalld

Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.

Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

猜您喜欢: