postgresql 常用操作

+++++++++++
登录:先切换到postgres

psql -p 4444 ##psql 后面不跟用户,默认就是以当前用户登录,如果你的端口改了,就要加上端口,没改不用加。


\list ##查看有那些数据库,等于mysql 的show databases;


\du ##查看用户列表


create user ysj createdb; ##建立用户,并授权



修改用户密码
alter user ysj password '123456';


也可以以这个用户,登录postgresql
直接运行\password 按回车,输入两次新的密码



psql -U ysj -h 192.168.189.131 -p 4444 -d testdb



对那个数据库授权,必须要进入到这个数据库。



ocean=# create user xyz with password 'Dfsc123321';
CREATE ROLE
ocean=# grant all privileges on database ocean to xyz;
GRANT
ocean=# grant all privileges on all tables in schema public to xyz;
GRANT
注意,该sql语句必须在所要操作的数据库里执行,这一句是将当前数据库下 public schema 的表都授权于 xyz

如果要单独一个权限以及单独一个表,则:
grant select on table mytable to xyz ##这一句就是将 mytable 这张表的查询权限授予 xyz



创建数据库:
create database testdb owner ysj; ##同时把创建的数据库的所有者设置为ysj 用户



数据库授权:
grant all privileges on database testdb to ysj; ## 在那个数据库上,对那个用户授权。



进入数据库:
\c testdb


以ysj用户登录psql
psql -U ysj -h 192.168.189.131 -p 4444 -d testdb



创建表:
 \d       #显示当前库下的所有表



4.2.3 创建test表
-bash-4.2$ psql -U bzh -h 10.0.0.14 -d testdb  #以bzh用户登录testdb库

testdb=# \d       #显示当前库下的所有表
testdb=# create table test (no int,name text );           #创建test表
列出表结构,等同于mysql 的 desc 表名
\d [table_name] 列出某一张表格的结构

直接 \d 显示所有表
\d test ##查看表设计结构


\c [database_name]
连接其他数据库,等于mysql 的,use 命令

insert into test(num,name) values(1001,'redhat');    #插入数据



testdb=# select * from test;      #查询test表中所有数据

删除数据:
delete from test where num='1001';

插入多行数据:
insert into test(num,name) values(1001,'centos'),(1002,'debian');



4.3 删除库和表

4.3.1 删除testdb库

postgres=# drop database testdb;

4.4 数据库的导入和导出

4.4.1 导出数据

语法

pg_dump -h localhost -U postgres(用户名) 数据库名(缺省时同用户名)   >/data/dum.sql

pg_dump -h localhost -U postgres(用户名) 数据库名(缺省时同用户名)  -t table(表名) >/data/dum.sql

将testdb库导出
[root@wangning ~]# pg_dump -h 10.0.0.14 -U bzh testdb >bzh.sql

指定端口号将tesdb库导出并压缩

[root@wangning ~]# pg_dump -h 10.0.0.14 -p 5432 -U bzh testdb|gzip >bzh.sql.gz

将test表导出

[root@wangning ~]# pg_dump -h 10.0.0.14 -U bzh testdb -t test >test.sql

++++++root 用户,shell界面,备份postgresql

su - postgres -s /bin/bash -c "pg_dump -h 192.168.189.199 -p 4444
-U ocean ocean >/tmp/ocean_20201110.sql"

4.4.2 导入数据

语法
psql -U postgres(用户名)  数据库名(缺省时同用户名) < /data/dum.sql
将bzh.sql导入testdb库中,testdb需要提前建好
[root@wangning ~]# psql -U bzh -h 10.0.0.14 testdb <bzh.sql
将bzh.sql.gz导入testdb库中,testdb需要提前建好

[root@wangning wangning]# gunzip -c bzh.sql.gz |psql -U postgres -h 10.0.0.14 -p 5432 testdb

+++++++++++++++
root用户下,导入psql备份:
su - postgres -s /bin/bash -c "psql -d ocean -f /tmp/ocean_20201110.sql"

5 常见报错

5.1 psql: FATAL:  database "bzh" does not exist


报错原因:如果登录时未指定连接的目标数据库,那么默认数据库名称与用户名相同。
++++++++++++++
+++++++++++ 备份数据
postgresql----> root用户,shell界面
su - postgres -s /bin/bash -c "pg_dump -h 192.168.189.199 -p 4444 -U ocean ocean >/tmp/ocean_20201110.sql"

mysql---->root用户,shell界面
mysqldump -uroot -p -P 3333 -B callcenter --triggers --master-data=2 --single-transaction -R -E >/tmp/callcenter20201110.sql



+++++++++++++++++++