cent os 安装使用postgresql

下载

yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-redhat11-11-2.noarch.rpm -y

安装

yum -y install postgresql11 postgresql11-server postgresql11-libs

初始化数据库

/usr/pgsql-11/bin/postgresql-11-setup initdb

设置开机自启动PostgreSQL和启动服务

systemctl enable postgresql-11
systemctl start postgresql-11
systemctl status postgresql-11

登录数据库,这里切换账号postgres

su - postgres
psql

Navicat连接PostgreSQL

这里要修改配置文件postgresql.conf

find / -name postgresql.conf
vi /var/lib/pgsql/11/data/postgresql.conf
找到listen_address那里,解开注释并修改引号内localhost的值为*
listen_address="*"

保存并退出,重启postgresql服务

systemctl restart postgresql-11

修改远程连接pg_hba.conf

find / -name pg_hba.conf
vi /var/lib/pgsql/11/data/pg_hba.conf

在文件末尾加上,如果不加上远程连接PostgreSQL会出现no pg_hba.conf…的错误

host all all 0.0.0.0/0 trust
在navicat连接,如果不修改localhost为*,navicat连接会提示错误“Connection Refuse”

我在这里修改了postgres用户的密码,步骤如下:

  • 切换用户后进入psql

su - postgres
psql

  • 修改密码

alter user postgres password ‘密码’

数据库基本操作

  • 新建数据库
    createdb mydb

  • 删除数据库
    dropdb mydb

  • 访问数据库 普通用户-命令行显示mydb=> 超级用户-命令行显示mydb=#
    psql mydb

常用命令

SELECT version();
SELECT current_date;

  • psql程序有许多不是 SQL 命令的内部命令。它们以反斜杠字符“\”开头。例如,您可以通过 typing 获取各种 PostgreSQL SQL 命令语法的帮助:
    查看帮助 \h
    退出连接 \q
  • 创建表
    CREATE TABLE products (
    product_no integer,
    name text,
    price numeric
    );
  • 插入数据
    INSERT INTO products (product_no, name, price) VALUES (1, ‘Cheese’, 9.99);
  • 更新数据
    UPDATE products SET price = 10 WHERE price = 5;
  • 删除数据
    DELETE FROM products WHERE price = 10;
  • 查询数据
    SELECT * from products;

参考文档
https://www.docs4dev.com/docs/zh/postgre-sql/10.7/reference/preface.html

你可能感兴趣的:(Linux系统编程)