官网
- https://www.postgresql.org
yum源
[root@gitlab ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
安装PostgreSQL服务
[root@gitlab ~]# yum install -y postgresql12 postgresql12-server
安装PostgreSQL 11就是 yum install postgresql11 postgresql11-server
安装PostgreSQL 9.5就是 yum install postgresql95 postgresql95-server
依此类推
初始化数据库
[root@gitlab ~]# /usr/pgsql-12/bin/postgresql-12-setup initdb Initializing database ... OK
启动PostgreSQL服务
[root@gitlab ~]# systemctl start postgresql-12 [root@gitlab ~]# systemctl enable postgresql-12 Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-12.service to /usr/lib/systemd/system/postgresql-12.service.
修改postgres账号密码
PostgreSQL安装成功之后,会默认创建一个名为postgres的Linux用户,初始化数据库后,会有名为postgres的数据库,来存储数据库的基础信息,例如用户信息等等,相当于MySQL中默认的名为mysql数据库。
postgres数据库中会初始化一名超级用户postgres
为了方便我们使用postgres账号进行管理,我们可以修改该账号的密码
进入PostgreSQL命令行
通过su命令切换linux用户为postgres会自动进入命令行
[root@gitlab ~]# su postgres bash-4.2$
启动SQL Shell
bash-4.2$ psql could not change directory to "/root": 权限不够 psql (12.4) Type "help" for help. postgres=#
修改密码
postgres=# ALTER USER postgres WITH PASSWORD 'www.centoscn.vip'; ALTER ROLE postgres=# quit bash-4.2$ exit exit
配置远程访问
开放端口
[root@gitlab ~]# firewall-cmd --add-port=5432/tcp --permanent success [root@gitlab ~]# firewall-cmd --reload success
修改IP绑定
[root@gitlab ~]# vi /var/lib/pgsql/12/data/postgresql.conf listen_addresses = '*' # what IP address(es) to listen on;
允许所有IP访问
[root@gitlab ~]# vi /var/lib/pgsql/12/data/pg_hba.conf host all all 0.0.0.0/0 md5 #末尾加入
重启PostgreSQL服务
[root@gitlab ~]# systemctl restart postgresql-12
PostgreSQL shell常用语法示例
- 启动SQL shell
[root@gitlab ~]# su postgres bash-4.2$ psql could not change directory to "/root": 权限不够 psql (12.4) Type "help" for help. postgres=#
- 创建数据库
postgres=# CREATE DATABASE mydb; CREATE DATABASE
- 查看所有数据库
postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- mydb | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (4 rows)
- 切换当前数据库
postgres=# \c mydb You are now connected to database "mydb" as user "postgres".
- 创建表
mydb=# CREATE TABLE test(id int,body varchar(100)); CREATE TABLE
- 查看当前数据库下所有表
mydb=# \d List of relations Schema | Name | Type | Owner --------+------+-------+---------- public | test | table | postgres (1 row)
用户与访问授权语法示例
- 新建用户
mydb=# CREATE USER test WITH PASSWORD 'test'; CREATE ROLE
- 赋予指定账户指定数据库所有权限
mydb=# GRANT ALL PRIVILEGES ON DATABASE mydb TO test; GRANT
- 移除指定账户指定数据库所有权限
REVOKE ALL PRIVILEGES ON DATABASE mydb TO test
权限代码:SELECT、INSERT、UPDATE、DELETE、TRUNCATE、REFERENCES、TRIGGER、CREATE、CONNECT、TEMPORARY、EXECUTE、USAGE
继续阅读
- 我的QQ
- QQ扫一扫
-
- 我的头条
- 头条扫一扫
-
评论