憨豆说安全 · 2020年08月10日

云芯一号教程 - PostgreSQL指导安装及基础使用教程

  PostgreSQL是一种免费的对象-关系型数据库管理系统(ORDBMS)。 PostgreSQL支持大部分的SQL标准并且提供了很多其他现代特性,如复杂查询、外键、触发器、视图、事务完整性、多版本并发控制等。同样,PostgreSQL也可以用许多方法扩展,例如通过增加新的数据类型、函数、操作符、聚集函数、索引方法、过程语言等。

1.配置安装PostgreSQL
1.1 安装PostgreSQL
  PostgreSQL可以直接用apt-get命令安装。

   sudo apt install postgresql-12

  安装成功后,会自动添加一个名为postgres的系统用户,密码随机,并自动生成一个名为postgres的数据库,用户名为postgres,密码随机,所以需要手动修改密码。

1.2 修改数据库密码
  修改postgres数据库密码,需要使用postgres用户登录数据库。

  sudo -u postgres psql
  postgres=# ALTER USER postgres WITH PASSWORD '123456';

1.3 修改系统用户密码
  修改postgres用户密码,需要切换到root用户下。

  jishu@Jishu:~$ su root
  Password: 
  root@Jishu:/home/jishu# passwd -d postgres
  passwd: password expiry information changed.
  root@Jishu:/home/jishu# sudo -u postgres passwd       
  Enter new UNIX password:   #输入新密码
  Retype new UNIX password:  #重新输入新密码
  passwd: password updated successfully

  postgres用户密码修改好之后,再切换到jishu用户后,就可以用命令访问PostgreSQL了。

  jishu@Jishu:~$ psql -U postgres -h 127.0.0.1
  Password for user postgres: 
  psql (12.2 (Ubuntu 12.2-3.pgdg18.04+1))
  SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384,    bits: 256, compression: off)
  Type "help" for help.
  
  postgres=#  

1.4 修改配置文件
  PostgreSQL默认监听本地地址访问,可以修改为监听任何地址访问,并启用密码验证。

  vim /etc/postgresql/12/main/postgresql.conf
  #修改listen_addresses项为
  listen_addresses = '*'
  #修改password_encryption项为
  password_encryption = md5

  vim /etc/postgresql/12/main/pg_hba.conf
  #在IPv4 local connections字段内添加监听的网段,0.0.0.0/0表示任何地址
  # IPv4 local connections:
  host    all     all     127.0.0.1/32            md5
  host    all     all     0.0.0.0/0               md5

  配置文件修改后,重启PostgreSQL服务生效。

  sudo service postgresql restart

  重新启动后,再次登录并查看数据库。

  jishu@Jishu:~$ psql -U postgres  -h 127.0.0.1  
  Password for user postgres: 
  psql (12.2 (Ubuntu 12.2-3.pgdg18.04+1))
  SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
  Type "help" for help.
  
  postgres=# \l
                          List of databases
     Name    |  Owner   | Encoding | Collate |  Ctype  |   Access  privileges   
  -----------+----------+----------+---------+---------+-----------------------
   postgres  | postgres | UTF8     | C.UTF-8 | C.UTF-8 | 
   template0 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
             |          |          |         |         | postgres=CTc/    postgres
   template1 | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
             |          |          |         |         | postgres=CTc/postgres
  (3 rows)

  PostgreSQL数据库数据库正常。

2.PostgreSQL数据库基本操作
2.1 数据库操作
  2.1.1 创建数据库

  CREATE DATABASE dbname;

  如果数据库不存在,则创建数据库;如数据库存在,则切换到指定数据库。

  2.1.2 选择数据库

  postgres=# \l
  ......
        数据库列表
  ......
  postgres=# \c dbname

  2.1.2 删除数据库

  DROP DATABASE [ IF EXISTS ] dbname;

  2.4 表操作
  2.4.1 创建表
  表的创建是数据库中非常重要的功能,基本格式如下:

  CREATE TABLE table_name(
     column1 datatype,
     column2 datatype,
     column3 datatype,
     .....
     columnN datatype,
     PRIMARY KEY( 一个或多个列 )
  );

  2.4.2 查看表结构

  postgres=# \c table_name

  2.4.3 删除表

  DROP TABLE table_name; 

  2.4.4 查询表数据

  SELECT 字段, 字段  from  table_name  where  条件;

  2.4.5 插入数据

  INSERT INTO table_name (column1, column2, column3,...columnN)
  VALUES (value1, value2, value3,...valueN);

  2.4.6 更新数据

  UPDATE table_name
  SET column1 = value1, column2 = value2...., columnN = valueN
  WHERE [condition];

  2.4.7 删除数据

  DELETE FROM table_name WHERE [condition];  

  2.4.8 查看表列表

  postgres=# \d


















推荐阅读
关注数
4271
内容数
71
低成本Arm微服务器开发平台“云芯1号”教程及应用,欢迎关注
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息