直接去PostgreSQL官网下载最新版本即可。
安装程序会一步步指导设置密码,设置数据保存路径等,注意:语言不要选择中文简体,要选择C
,不然安装中会出错!
安装完成后把PostgreSQL的bin路径添加到环境变量中,这样在cmd命令行可以直接启动psql.exe
(操纵数据库的工具)
win+r,输入cmd打开命令行,输入:
> psql.exe -U postgres
接着输入安装时设置的密码,即可连接上数据库,此时界面出现了:
postgres=#
postgres=# create database shop
执行成功,会出现CREATE DATABASE
postgres=# \q
首先进入shop数据库:
> psql.exe -U postgres -d shop
输入密码后会出现:
shop=#
接着建表并插入数据:
shop=# create table Product
shop-# (product_id CHAR(4) NOT NULL,
shop(# product_name VARCHAR(100) NOT NULL,
shop(# product_type VARCHAR(32) NOT NULL,
shop(# sale_price INTEGER,
shop(# purchase_price INTEGER,
shop(# regist_date DATE,
shop(# PRIMARY KEY(product_id));
shop=# begin transaction;
shop=# insert into Product values('0001','Tshirt','衣服',1000,500,'2019-09-20');
shop=# insert into Product values('0002','打孔器','办公用品',500,320,'2019-09-11');
shop=# INSERT INTO Product VALUES ('0003', '运动T恤', '衣服', 4000, 2800, NULL);
shop=# INSERT INTO Product VALUES ('0004', '菜刀', '厨房用具', 3000, 2800, '2009-09-20');
shop=# INSERT INTO Product VALUES ('0005', '高压锅', '厨房用具', 6800, 5000, '2009-01-15');
shop=# INSERT INTO Product VALUES ('0006', '叉子', '厨房用具', 500, NULL, '2009-09-20');
shop=# INSERT INTO Product VALUES ('0007', '擦菜板', '厨房用具', 880, 790, '2008-04-28');
shop=# INSERT INTO Product VALUES ('0008', '圆珠笔', '办公用品', 100, NULL,'2009-11-11');
> install.packages("RPostgreSQL")
> library(RPostgreSQL)
> pgdb_con <- dbConnect(RPostgreSQL::PostgreSQL(),user="postgres",password="####",dbname="shop",host="localhost")