1. 安装
sudo apt install postgresql
sudo apt install pgadmin3
2. 命令
sudo -u postgres psql # 切换到postgres用户进入
\password postgres # 修改密码
CREATE USER dbuser WITH PASSWORD 'password'; # 创建新用户
GRANT ALL PRIVILEGES ON DATABASE exampledb to dbuser; # 赋予权限
- SHELL 操作
sudo -u postgres createuser --superuser dbuser # 创建超级用户
sudo -u postgres psql # 修改密码
\password dbuser
\q
sudo -u postgres createdb -O dbuser exampledb # 创建数据库并赋予权限
psql -U dbuser -d exampledb -h 127.0.0.1 -p 5432
sudo passwd -d postgres
sudo -u postgres passwd
\encoding
# 创建数据库
create database xx
# 删除数据库
dropdb xx
# 备份数据库
pg_dump xx> /usr/local/pgsql/backup/xx.dmp
# 恢复数据库
psql xx < /usr/local/pgsql/backup/xx.dmp
# 创建新表
CREATE TABLE user_tbl(name VARCHAR(20), signup_date DATE);
# 插入数据
INSERT INTO user_tbl(name, signup_date) VALUES('张三', '2013-12-22');
# 选择记录
SELECT * FROM user_tbl;
# 更新数据
UPDATE user_tbl set name = '李四' WHERE name = '张三';
# 删除记录
DELETE FROM user_tbl WHERE name = '李四' ;
# 添加栏位
ALTER TABLE user_tbl ADD email VARCHAR(40);
# 更新结构
ALTER TABLE user_tbl ALTER COLUMN signup_date SET NOT NULL;
# 更名栏位
ALTER TABLE user_tbl RENAME COLUMN signup_date TO signup;
# 删除栏位
ALTER TABLE user_tbl DROP COLUMN email;
# 表格更名
ALTER TABLE user_tbl RENAME TO backup_tbl;
# 删除表格
DROP TABLE IF EXISTS backup_tbl;
# 启动
sudo su
su postgres
psql
\c dqchinaweb
\d # 列出所有表
# 查看表结构
\d economic_dqchina_xiezilouinfo_id_seq
# 更新表数据
update economic_dqchina_xiezilouinfo set id = id + 2000000;
# 更新id_seq表
alter sequence economic_dqchina_xiezilouinfo_id_seq restart with 2000337;
# 数据库中设置id自增的初始值
select setval('testtable_id_seq',1000,false); # 表名自己查,对应id_seq表
# 查询最大值,最小值,数量
select max(hotel_id) from dt_crawler_hotel;
select min(hotel_id) from dt_crawler_hotel;
select count(hotel_id) from dt_crawler_hotel;
# 删除
delete from t_user where user_id='12345';
# 配置可以直接用postgres用户登录
sudo vim /etc/postgresql/9.5/main/pg_hba.conf
local all postgres peer
Should be
local all postgres md5
# 中心点更改(st_centroid)
UPDATE dt_crawler_businesscircle SET center_point = st_centroid(boundary);