文档
https://www.postgresql.org/docs/14/tutorial-sql.html
安装
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
sudo yum install -y postgresql15-server
sudo /usr/pgsql-15/bin/postgresql-15-setup initdb
sudo systemctl enable postgresql-15
sudo systemctl start postgresql-15
修改密码
sudo passwd -d postgres
sudo -u postgres passwd
使用
启用客户端
sudo su postgres
创建数据库
psql createdb mydb
删除数据库
psql dropdb mydb
进入数据库
psql mydb
退出数据库
\q
创建数据库
CREATE TABLE weather (
city varchar(80),
temp_lo int, -- low temperature
temp_hi int, -- high temperature
prcp real, -- precipitation
date date
);
删除表
DROP TABLE weather;
写入数据
INSERT INTO weather (city, temp_lo, temp_hi, prcp, date)VALUES ('San Francisco', 43, 57, 0.0, '1994-11-29');
查询数据
SELECT * FROM weather;
修改
UPDATE weather SET temp_hi = temp_hi - 2, temp_lo = temp_lo - 2 WHERE date > '1994-11-28';
删除
DELETE FROM weather WHERE city = 'Hayward';
事务
BEGIN;
UPDATE accounts SET balance = balance - 100.00
WHERE name = 'Alice';
-- etc etc
COMMIT;