PostgreSQL入门-安装与基本使用(Ubuntu)

朴灿烈づ我的快乐病毒、 2023-06-12 09:17 68阅读 0赞

PostgreSQL入门-安装与基本使用(Ubuntu)

PostgreSQL 是一个免费的对象-关系数据库服务器(ORDBMS),号称是 “世界上最先进的开源关系型数据库”。

PostgreSQL 是以加州大学计算机系开发的 POSTGRES 4.2版本为基础的对象关系型数据库。

今天在Ubuntu系统上,我们一起来安装并简单使用一下PostgreSQL数据库。

1.查看当前系统版本:

  1. $ cat /etc/issue
  2. Ubuntu 16.04.6 LTS \n \l
  3. $ sudo lsb_release -a
  4. LSB Version:
  5. core-9.20160110
  6. ubuntu0.2-amd64:core-9.20160110
  7. ubuntu0.2-noarch:security-9.20160110
  8. ubuntu0.2-amd64:security-9.20160110
  9. ubuntu0.2-noarch
  10. Distributor ID: Ubuntu
  11. Description: Ubuntu 16.04.6 LTS
  12. Release: 16.04
  13. Codename: xenial

系统是 Ubuntu 16.04.6 LTS。

2.安装 PostgreSQL

  1. $ sudo apt-get install postgresql

执行实例如下:

  1. $ sudo apt-get install postgresql
  2. Reading package lists... Done
  3. Building dependency tree
  4. Reading state information... Done
  5. The following additional packages will be installed:
  6. libpq5
  7. postgresql-9.5
  8. postgresql-client-9.5
  9. postgresql-client-common
  10. postgresql-common
  11. postgresql-contrib-9.5
  12. ssl-cert
  13. Creating config file /etc/postgresql-common/createcluster.conf with new version
  14. Creating config file /etc/logrotate.d/postgresql-common with new version
  15. Building PostgreSQL dictionaries from installed myspell/hunspell packages...
  16. Removing obsolete dictionary files:
  17. Setting up postgresql-9.5 (9.5.19-0ubuntu0.16.04.1) ...
  18. Creating new cluster 9.5/main ...
  19. config /etc/postgresql/9.5/main
  20. data /var/lib/postgresql/9.5/main
  21. locale en_US.UTF-8
  22. socket /var/run/postgresql
  23. port 5432
  24. update-alternatives: using /usr/share/postgresql/9.5/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode
  25. Setting up postgresql (9.5+173ubuntu0.2) ...
  26. Setting up postgresql-contrib-9.5 (9.5.19-0ubuntu0.16.04.1) ...
  27. Processing triggers for libc-bin (2.23-0ubuntu11) ...
  28. Processing triggers for ureadahead (0.100.0-19.1) ...
  29. Processing triggers for systemd (229-4ubuntu21.21) ...

默认已经安装了 postgresql 的服务器(postgresql-9.5)和客户端(postgresql-client-9.5)。

2019年10月03日,已经发布了PostgreSQL 12,如果想安装最新版的,需要更新一下源,参加 PostgreSQL Apt Repository

可以使用 psql --version 来查看当前安装的版本:

  1. $ psql --version
  2. psql (PostgreSQL) 9.5.19

安装后会默认生成一个名为 postgres的数据库和一个名为postgres的数据库用户。

同时还生成了一个名为 postgres 的 Linux 系统用户。

可以使用以下命令查看:

  1. #查看用户
  2. $ cat /etc/passwd
  3. #查看用户组
  4. $ cat /etc/group

3.使用PostgreSQL控制台修改 postgres 数据库用户密码

默认生成的 postgres 的数据库用户没有密码,现在我们使用 postgres Linux用户的身份来登录到管理控制台中。

  1. # 切换到postgres用户。
  2. $ sudo su - postgres
  3. postgres@iZm5e8p54dk31rre6t96xuZ:~$
  4. postgres@iZm5e8p54dk31rre6t96xuZ:~$ whoami
  5. postgres

Linux 用户 postgres 以同名的 postgres 数据库用户的身份登录,不用输入密码的。

  1. postgres@iZm5e8p54dk31rre6t96xuZ:~$ psql
  2. psql (9.5.19)
  3. Type "help" for help.
  4. postgres=#

使用 \password 命令,为 postgres 用户设置一个密码

  1. postgres=#
  2. postgres=# CREATE USER db_user WITH PASSWORD 'PWD123456';
  3. CREATE ROLE
  4. postgres=#

创建用户数据库,这里为testdb,并指定所有者为db_user。

  1. postgres=# CREATE DATABASE testdb OWNER db_user;
  2. CREATE DATABASE
  3. postgres=#

将 testdb 数据库的所有权限都赋予 db_user 数据库用户, 否则 db_user 只能登录控制台,没有数据库操作权限。

  1. postgres=# GRANT ALL PRIVILEGES ON DATABASE testdb TO db_user;
  2. GRANT

使用 \du 查看当前的数据库用户:

  1. postgres=# \du;
  2. List of roles
  3. Role name | Attributes | Member of
  4. -----------+------------------------------------------------+-----------
  5. db_user | | {}
  6. postgres | Superuser,Create role,Create DB,Replication,Bypass RLS | {}

最后,使用 \q 命令退出控制台, 并使用 exit 命令退出当前 db_user Linux用户。

  1. postgres=# \q
  2. postgres@iZm5e8p54dk31rre6t96xuZ:~$
  3. postgres@iZm5e8p54dk31rre6t96xuZ:~$ exit
  4. logout

4.数据库基本操作实例

创建数据库与删除数据库:

  1. # 创建数据库
  2. postgres=# CREATE DATABASE lusiadas;
  3. CREATE DATABASE
  4. # 删除数据库
  5. postgres=# DROP DATABASE lusiadas;
  6. DROP DATABASE

使用 \c 切换数据库:

  1. postgres=# CREATE DATABASE testdb;
  2. CREATE DATABASE
  3. postgres=# \c testdb;
  4. SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
  5. You are now connected to database "testdb" as user "postgres".

新建表与删除表:

  1. # 创建一个表 tb_test:(两个字段,其中id 为自增ID)
  2. testdb=> CREATE TABLE tb_test(id bigserial, name VARCHAR(20));
  3. CREATE TABLE
  4. # 删除一个表 tb_test
  5. testdb=> DROP table tb_test;
  6. DROP TABLE

增删改查操作:

  1. # 创建一个用户表 tb_users(三个字段,其中id 为自增ID)
  2. testdb=> CREATE TABLE tb_users(id bigserial, age INT DEFAULT 0, name VARCHAR(20));
  3. CREATE TABLE
  4. # 使用 INSERT 语句插入数据
  5. testdb=> INSERT INTO tb_users(name, age) VALUES('张三丰', 212);
  6. INSERT 0 1
  7. testdb=> INSERT INTO tb_users(name, age) VALUES('李四光', 83);
  8. INSERT 0 1
  9. testdb=> INSERT INTO tb_users(name, age) VALUES('王重阳', 58);
  10. INSERT 0 1
  11. # 查询数据
  12. testdb=> select * from tb_users;
  13. id | age | name
  14. ----+-----+--------
  15. 1 | 212 | 张三丰
  16. 2 | 83 | 李四光
  17. 3 | 58 | 王重阳
  18. (3 rows)
  19. testdb=> select * from tb_users WHERE id=3;
  20. id | age | name
  21. ----+-----+--------
  22. 3 | 58 | 王重阳
  23. (1 row)
  24. # 更新数据 (执行后输出更新的条数,第二次执行失败所以输出为`UPDATE 0`)
  25. testdb=> UPDATE tb_users set name = '全真派王重阳' WHERE name = '王重阳';
  26. UPDATE 1
  27. testdb=> UPDATE tb_users set name = '全真派王重阳' WHERE name = '王重阳';
  28. UPDATE 0
  29. # 插入2条数据
  30. testdb=> INSERT INTO tb_users(name, age) VALUES('赵四', 0);
  31. INSERT 0 1
  32. testdb=> INSERT INTO tb_users(name, age) VALUES('赵五娘', 0);
  33. INSERT 0 1
  34. # 模糊查询
  35. testdb=> SELECT * FROM tb_users WHERE name LIKE '赵%';
  36. id | age | name
  37. ----+-----+--------
  38. 4 | 0 | 赵五娘
  39. 5 | 0 | 赵四
  40. (2 rows)
  41. # 修改表结构: 新增字段
  42. testdb=# ALTER TABLE tb_users ADD email VARCHAR(50);
  43. ALTER TABLE
  44. # 修改表结构: 修改字段
  45. testdb=# ALTER TABLE tb_users ALTER COLUMN email TYPE VARCHAR(100);
  46. ALTER TABLE
  47. # 删除字段
  48. testdb=# ALTER TABLE tb_users DROP COLUMN email;
  49. ALTER TABLE
  50. # 删除记录
  51. testdb=> DELETE FROM tb_users WHERE id = 5;
  52. DELETE 1

使用 pg_database_size() 查看数据库的大小:

  1. testdb=# select pg_database_size('testdb');
  2. pg_database_size
  3. ------------------
  4. 7991967
  5. (1 row)
  6. testdb=# select pg_size_pretty(pg_database_size('testdb'));
  7. pg_size_pretty
  8. ----------------
  9. 7805 kB
  10. (1 row)

5.PostgreSQL 的 timestamp 类型

查询 current_timestamp

  1. testdb=# select current_timestamp;
  2. current_timestamp
  3. -------------------------------
  4. 2019-11-11 08:33:35.369887+00
  5. (1 row)

使用 current_timestamp(0) 定义时间类型精度为0:(有时区)

  1. testdb=# select current_timestamp(0);
  2. current_timestamp
  3. ------------------------
  4. 2019-11-11 08:31:08+00
  5. (1 row)

使用 current_timestamp(0) 定义时间类型精度为0:(去掉时区)

  1. testdb=# select current_timestamp(0)::timestamp without time zone;
  2. current_timestamp
  3. ---------------------
  4. 2019-11-11 08:31:20
  5. (1 row)
  6. testdb=# select cast (current_timestamp(0) as timestamp without time zone);
  7. current_timestamp
  8. ---------------------
  9. 2019-11-11 08:32:26
  10. (1 row)

时间戳:

  1. testdb=# select extract(epoch from now());
  2. date_part
  3. ------------------
  4. 1573461495.47821
  5. (1 row)

设置数据库时区:

视图 pg_timezone_names 保存了所有可供选择的时区:

  1. # 查看时区
  2. select * from pg_timezone_names;

比如可以选择上海 Asia/Shanghai 或重庆 Asia/Chongqing, 最简单的直接 PRC:

  1. testdb=# set time zone 'PRC';
  2. SET
  3. testdb=# show time zone;
  4. TimeZone
  5. ----------
  6. PRC
  7. (1 row)
  8. testdb=# SELECT LOCALTIMESTAMP(0);
  9. localtimestamp
  10. ---------------------
  11. 2019-11-11 16:42:54
  12. (1 row)

Reference

https://www.postgresql.org/docs/8.4/sql-altertable.html
http://www.ruanyifeng.com/blog/2013/12/getting\_started\_with\_postgresql.html

[END]

发表评论

表情:
评论列表 (有 0 条评论,68人围观)

还没有评论,来说两句吧...

相关阅读