PostgreSQL和MySQL的语法区别

 

PostgreSQL和MySQL的语法区别能有多大呢?带着这个疑问,我在本地同时安装 PostgreSQL 和 MySQL ,进行了基础的语法操作。结果 PostgreSQL 的操作出乎我的想象。

安装和启动命令:

brew install postgresql

brew services start postgresql

psql postgres

启动之后

➜ miyu psql postgres
psql (14.4)
Type “help” for help.

postgres=# ?
postgres-# \help

比 MySQL 终端界面

MariaDB [idiom]>

要简陋一点。

想当然的用 MySQL 的显示全部数据库命令 show databases 列出所有数据库 ,但没成功。原来是需要用 \l 

postgres-# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
———–+——-+———-+———+——-+——————-
postgres | c | UTF8 | C | C |
template0 | c | UTF8 | C | C | =c/c +
| | | | | c=CTc/c
template1 | c | UTF8 | C | C | =c/c +
| | | | | c=CTc/c
(3 rows)

创建数据库

 

postgres-# create database idiom;

再次查看已经多了一个数据库 idiom.

postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
———–+——-+———-+———+——-+——————-
idiom | c | UTF8 | C | C |
postgres | c | UTF8 | C | C |
template0 | c | UTF8 | C | C | =c/c +
| | | | | c=CTc/c
template1 | c | UTF8 | C | C | =c/c +
| | | | | c=CTc/c
(4 rows)

切换到 idiom 数据库 \c 

postgres-# \c idiom;
You are now connected to database “idiom” as user “c”.

新建一张表,表名为  idiom

idiom=# create table idiom (id CHAR(20) primary key not null ,first_word char(1) not null,second_word char(1) not null,third_word char(1) not null,fourth_word char(1) not null);

PostgreSQL 中用 \d 查看建表结构 ,比  MySQL 的 show create table  命令短一些有没有?

idiom=# \d idiom
id | character(20) | | not null |
first_word | character(1) | | not null |
second_word | character(1) | | not null |
third_word | character(1) | | not null |
fourth_word | character(1) | | not null |

往 PostgreSQL 的某张表插入一条新纪录

idiom=# insert into idiom (id,first_word,second_word,third_word,fourth_word) values (‘3432′,’一’,’心’,’一’,’意’);

PostgreSQL 查询记录

idiom=# select * from idiom;
id | first_word | second_word | third_word | fourth_word
———————-+————+————-+————+————-
3432 | 一 | 心 | 一 | 意
(1 row)

根据条件查询 PostgreSQL 记录

idiom=# select * from idiom where id = ‘3432’;
id | first_word | second_word | third_word | fourth_word
———————-+————+————-+————+————-
3432 | 一 | 心 | 一 | 意
(1 row)

更新一条记录

idiom=# update idiom set first_word=’石’,second_word=’破’,third_word=’天’,fourth_word=’惊’ where id = ‘3432’;
UPDATE 1
idiom=# select * from idiom;
id | first_word | second_word | third_word | fourth_word
———————-+————+————-+————+————-
3432 | 石 | 破 | 天 | 惊
(1 row)

 

删除一条记录

idiom=# delete from idiom where id=’3432′;
DELETE 1

额,看上去增删改查的操作指令和 MySQL没啥区别的操作。如果要从创建一个新项目使用 PostgreSQL 替代  MySQL 其实心智没啥负担。后面的项目尝试一下上 PostgreSQL 吧。

Linux 版本的 PostgreSQL

在Linux 系统,比如 Centos 系统下安装 PostgreSQL 很简单。

 

简单的几条安装加默认启动命令

yum install postgresql-server postgresql-contrib

postgresql-setup initdb

systemctl start postgresql

systemctl enable postgresql

 

安装完成之后,设置密码。

passwd postgres

由于 PostgreSQL 默认需要切换到 postgres 用户进行登录。

su postgres

psql postgres

postgres=#

这样就可以登录进来了,其他操作就是和 Mac 端已经安装好的一模一样了。

 

PostgreSQL 客户端远程连接

 

需要修改配置

vim /var/lib/pgsql/data/postgresql.conf


#去掉listen_address和port前面的注释,并叫localhost改成*
listen_addresses = '*'         
port = 5432                            

创建远程连接用户,

create user idiomuser createdb login password ‘TTT000123’; # createdb 是权限

grant all on database idiomdb to idiomuser;

并需要配置远程连接认证

# 编辑vim /var/lib/pgsql/data/pg_hba.conf文件,将原来的local和host注释掉,并加入以下内容
local   all             all                                     md5
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
host    all             all             0.0.0.0/0               md5

systemctl restart postgresql 重启一下就起作用了。

 

远程连接试试

psql -h 192.168.1.250 -U idiom

使用了云服务器的话,比如腾讯云的话,官网 说经文档还需要自己配置一个安全组。【连接云数据库 PostgreSQL 须开通5432协议端口。】