postgresql在linux和windows安装和入门基础教程(代码片段)

共饮一杯无 共饮一杯无     2022-11-29     787

关键词:

StackOverflow 2022 开发者报告:总体而言,MySQL 依然是最受欢迎的的数据库。但在专业开发者群体中,PostgreSQL(46.48%)已经超越 MySQL(45.68%)夺得了第一名。看来必须得学一波了啊。


PostgreSQL是一种特性非常齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库管理系统。POSTGRES的许多领先概念只是在比较迟的时候才出现在商业网站数据库中。PostgreSQL支持大部分的SQL标准并且提供了很多其他现代特性,如复杂查询、外键、触发器、视图、事务完整性、多版本并发控制等。同样,PostgreSQL也可以用许多方法扩展,例如通过增加新的数据类型、函数、操作符、聚集函数、索引方法、过程语言等。另外,因为许可证的灵活,任何人都可以以任何目的免费使用、修改和分发PostgreSQL。
今天给大家带来PostgreSQL安装和一些基础教程。

安装和使用

https://www.runoob.com/postgresql/windows-install-postgresql.html
打开PostgreSQL官网地址:https://www.postgresql.org/,点击上方的Download。可以看到很多平台的安装包,有Linux,macOS,Windows,BSD,Solaris。

Linux 上安装 PostgreSQL

选择上方的Linux后可以看到多种Linux平台,这里我选择Red Hat/Rocky/Centos这个,选择后进入页面可以看到对应的yum语句。

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 postgresql14-server
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14

Windows 上安装 PostgreSQL


选择Windows版本的下载后,点击链接进入EnterpriseDB网站进行下载https://www.enterprisedb.com/downloads/postgres-postgresql-downloads

这里我选择64位的Windows系统安装包进行下载。下载完成后点击对应的安装包进行安装。

选择对应的安装位置。

选择需要安装组件。

选择数据库路径。

设置超级用户的密码,我这里设置的是zjqzjq。

设置端口号,我这里使用默认的5432。

后续继续next进行安装了。

安装完成后点击取消勾选,点击finish完成安装。

打开pgAdmin4(可以理解成Navicat这种数据库连接工具。)

进入界面后,点击左侧servers下面的postgresql 14,输入密码。

进入后界面如下:

打开SQL shell也可以进入执行相关SQL操作。

前面几个使用默认的,用户口令使用上面设置的密码。

基础使用

增删改查

表操作

创建表:

-- 创建一张表并设置唯一索引和普通索引
create table book(
    id serial primary key ,
    title text not null ,
    meta jsonb default ::jsonb,
    price money,
    isbn text not null ,
    publish_at date not null 
);
create unique index on book(isbn);
create index on book using gin(meta);

-- 创建表,含一个自增主键(自增序列底层逻辑:绑定 sequence 对象的表达式默认值),一个content字段保存订单详情,一个时间戳字段记录订单入库时间
create table trade (
    id serial primary key,
    content text,
    created_at timestamp default now()
);

删除表:

数据操作

新增数据:

insert into book(title, price, isbn, publish_at) select a book title, 25.4, xx-xxxx-xxxx, 2019-12-1::date;
insert into book(title, price, isbn, publish_at) select a other book title, 25.4, yy-yyyy-xxxx, 2019-12-1::date; 

更新数据:

create table employee
(
    id serial primary key,
    name text,
    dept text,
    salary money
);

-- 修改销售部(dept 字段为 sale)员工 Dora Muk 的工资,将其增加 1000,返回她的工号。
update employee set salary = salary + 1000 where dept = sale and name = Dora Muk returning id;

删除数据:

create table orders
(
    id         serial primary key,
    meta       jsonb     default ::jsonb,
    content    jsonb     default ::jsonb,
    created_at timestamp default now(),
    deal       boolean
)
-- 删除deal为true的数据并返回id
delete from orders where deal returning id;

查询:

-----------------------------子查询、分组、联合查询
create table employee
(
    id     serial primary key,
    name   text,
    dept   text,
    salary money
);
-- 查询出每个部门工资最高的员工的 id, name, dept, salary
select l.id, l.name, l.dept, l.salary
from employee as l
join (select max(salary) as salary, dept
       from employee
       group by dept) as r
on l.dept = r.dept and l.salary = r.salary

-- 找出比销售部(dept 为 sale)工资最高的员工工资更高的那部分人,查询出他们的完整信息
select id, name, dept, salary
from employee
where salary > (select max(salary)
                from employee
                where dept = sale)

------------------- 分页查询------------------------
create table orders
(
    id          serial primary key,
    product_id  integer,
    order_date  date default now(),
    quantity    integer,
    customer_id integer
);

-- 查询指定的某一天内的数据,并按每一百条一页查询
select id, product_id, order_date, quantity, customer_id
from orders
where date = $1
offset $2 limit 100; 

-----------树结构述根---------------------
create table node
(
    id      serial primary key,
    pid     integer,
    content text
);

-- 其 pid 列引用 id 列,形成一个树结构,根节点的 pid 为 0。写一个查询,找到某一个给定id的记录,其父节点、父节点的父节点,直至根节点的路径。

函数

plsql支持多种函数类型,函数可以用 SQL 写,也可以用 PLPGSQL,还可以用 Python、Perl、LUA等语言。

  1. 支持数学操作符(除(/)、取模(%)、阶乘(!!)、绝对值(@)等。)
  2. 支持位串操作符(按位左移(<< )、按位右移(>>)、按位AND(&)等。)
  3. 支持数学函数(绝对值(abs(x))、0.0到1.0之间的随机数值(random())、余弦(cos(x))等。)
  4. 支持字符串函数(字串连接(string || string)、合并字符串(concat(串1,串2…))、string中字符的数目(length(string text))等。)
  5. 支持数据类型格式化函数
    to_char(timestamp, text) 把时间戳转换成字串 to_char(current_timestamp, HH12:MI:SS)
    to_char(interval, text) 把时间间隔转为字串 to_char(interval 15h 2m 12s, HH24:MI:SS)
    to_char(int, text) 把整数转换成字串 to_char(125, 999)
    to_char(double precision, text) 把实数/双精度数转换成字串 to_char(125.8::real, 999D9)
    to_char(numeric, text) 把numeric转换成字串 to_char(-125.8, 999D99S)
    to_date(text, text) 把字串转换成日期 to_date(05 Dec 2000, DD Mon YYYY)
    to_timestamp(text, text) 把字串转换成时间戳 to_timestamp(05 Dec 2000, DD Mon YYYY)
    to_timestamp(double) 把UNIX纪元转换成时间戳 to_timestamp(200120400)
    to_number(text, text) 把字串转换成numeric to_number(12,454.8-, 99G999D9S)
  6. 支持日期/时间函数
    age(timestamp, timestamp) 减去参数,生成一个使用年、月的"符号化"的结果 age(2001-04-10, timestamp 1957-06-13) 43 years 9 mons 27 days
    age(timestamp) 从current_date减去得到的数值 age(timestamp 1957-06-13) 43 years 8 mons 3 days
    current_date 今天的日期
    current_time 现在的时间
    current_timestamp 日期和时间
    date_part(text, timestamp) 获取子域(等效于extract) date_part(hour, timestamp 2001-02-16 20:38:40) 20
    date_part(text, interval) 获取子域(等效于extract) date_part(month, interval 2 years 3 months) 3
    date_trunc(text, timestamp) 截断成指定的精度 date_trunc(hour, timestamp 2001-02-16 20:38:40) 2001-02-16 20:00:00+00
    extract(field from timestamp) 获取子域 extract(hour from timestamp 2001-02-16 20:38:40) 20
    extract(field from interval) 获取子域 extract(month from interval 2 years 3 months) 3
    localtime 今日的时间
    localtimestamp 日期和时间
    now() 当前的日期和时间(等效于 current_timestamp)
    timeofday() 当前日期和时间
  7. 系统信息函数
    current_database() 当前数据库的名字
    current_schema() 当前模式的名字
    current_schemas(boolean) 在搜索路径中的模式名字
    current_user 目前执行环境下的用户名
    inet_client_addr() 连接的远端地址
    inet_client_port() 连接的远端端口
    inet_server_addr() 连接的本地地址
    inet_server_port() 连接的本地端口
    session_user 会话用户名
    pg_postmaster_start_time() postmaster启动的时间
    user current_user
    version() PostgreSQL版本信息
    has_table_privilege(user,table,privilege) 用户是否有访问表的权限 SELECT/INSERT/UPDATE/DELETE/RULE/REFERENCES/TRIGGER 允许用户在程序里查询对象访问权限的函数
    has_table_privilege(table,privilege) 当前用户是否有访问表的权限 SELECT/INSERT/UPDATE/DELETE/RULE/REFERENCES/TRIGGER
    has_database_privilege(user,database,privilege) 用户是否有访问数据库的权限 CREATE/TEMPORARY
    has_database_privilege(database,privilege) 当前用户是否有访问数据库的权限 CREATE/TEMPORARY
    has_function_privilege(user,function,privilege) 用户是否有访问函数的权限 EXECUTE
    has_function_privilege(function,privilege) 当前用户是否有访问函数的权限 EXECUTE
    has_language_privilege(user,language,privilege) 用户是否有访问语言的权限 USAGE
    has_language_privilege(language,privilege) 当前用户是否有访问语言的权限 USAGE
    has_schema_privilege(user,schema,privilege) 用户是否有访问模式的权限 CREAT/USAGE
    has_schema_privilege(schema,privilege) 当前用户是否有访问模式的权限 CREAT/USAGE
    has_tablespace_privilege(user,tablespace,privilege) 用户是否有访问表空间的权限 CREATE

上述函数仅部分,更多查看官方:https://www.postgresql.org/docs/

用户和权限

授权:

-- 给用户 fred 授权,允许他查询 emplyee 表
grant select on table employee to fred;

撤销权限:

-- 撤销用户fred 对 trade 表的查询权限
revoke select on trade from fred;

角色授权:

-- Fred、Alice、James、Jone 四位成员,现在你需要给数据分析组授权,允许他们 查询 trade 数据库的 public schema 中的所有表.
create role analysis;
grant analysis to fred, alice, james, jone;
grant select on all tables in schema public to analysis;

索引和约束

索引:

create table book(
    id serial primary key ,
    title text not null ,
    meta jsonb default ::jsonb,
    price money,
    isbn text not null ,
    publish_at date not null 
);
create unique index on book(isbn);
create index on book using gin(meta);

有兴趣可以看看我写的mysql相关语句和命令汇总:
长文一次说完MySQL常用语句和命令等汇总
也可以参考菜鸟教程的postgresql教程:https://www.runoob.com/postgresql/postgresql-tutorial.html

怎样在虚拟机上的linux建立postgresql数据库,然后在windows访问?

...谢谢。。。主要是测试linux数据库,windows访问的。。用的postgresql。具体怎么弄、、、为何我linux上postgresql配好了,也成功运行了,第二天又出现无法编译。。因为第二天启动不了数据库啊,所以重新initdb一下用的redhat9,刚才不... 查看详情

如何在 Windows 上仅安装 PostgreSQL 的客户端工具?

】如何在Windows上仅安装PostgreSQL的客户端工具?【英文标题】:HowdoIinstalljusttheclienttoolsforPostgreSQLonWindows?【发布时间】:2016-02-2414:24:38【问题描述】:我在Linux系统上有一个PostgreSQL数据库,我想从我的WindowsPC访问它。但我能找到... 查看详情

xmanager5安

...软件内容Xmanager PCX ServerXmanager是一个运行于MS Windows平台上的高性能的X window服务器。可以在本地PC上同时运行Unix/Linux和Windows图形应用程序。XshellXshell是一个强大的SSH,TELNET,和RLOGIN终端仿真软件。它使得用户能轻... 查看详情

如何在windows10中安装postgresql和连接设置

本文介绍开源数据库PostgreSQL(版本9.5.31)在Windows10环境下的安装方法。PostgreSQL是1980年以加利福尼亚大学为中心开发出来的DBMS,与MySQL一样,都是世界上广泛应用的开源数据库(DB)。它严格遵守标准SQL规则,是初学者的最佳选 查看详情

使用 Python 在 Windows 中构建 postgreSQL

】使用Python在Windows中构建postgreSQL【英文标题】:BuildpostgreSQLinWindowswithPython【发布时间】:2014-06-0620:54:37【问题描述】:我可以在Windows中使用Python从源代码安装和构建postgreSQL吗?坚固吗?目前在他们的文档中,他们有VisualC++asth... 查看详情

PostgreSQL 9.2.4-x64 在 windows 8 Pro 64bit 上的安装问题

】PostgreSQL9.2.4-x64在windows8Pro64bit上的安装问题【英文标题】:PostgreSQL9.2.4-x64installationproblemsonwindows8Pro64bit【发布时间】:2013-05-2407:31:15【问题描述】:我正在尝试在Windows8Pro64上安装PostgreSQL9.2.4,但安装问题接近尾声时,我一直收... 查看详情

如何在 Linux 上的 XAMPP 中启用 PostgreSQL [关闭]

】如何在Linux上的XAMPP中启用PostgreSQL[关闭]【英文标题】:HowcanIenablePostgreSQLinXAMPPonLinux[closed]【发布时间】:2014-08-2303:21:43【问题描述】:我是Linux的新用户,我遇到了一些问题。我不知道如何在Linux上的XAMPP中启用PostgreSQL。我在W... 查看详情

postgresql和sqlserver哪个更好些?(作为平时学习之用)

...QL自带的企业管理器、Management感觉容易一些!也很方便!POSTGRESQL使用PGADMIN管理,麻烦一点!POSTGRESQL在windows下稳定性不够!在LINUX下则极稳定!如果在WINDOWS下自学还是MSSQL吧追问最后这连个我都装了,因为我学的相关教材这两个... 查看详情

windows下有postgresql的pgbench吗

...h.exe_913735.html参考技术A执行下列步骤前,暂作以下假定:PostgreSQL拟装在d:\postgresql数据库以后拟存放在d:\postgresql\data首先下载postgresql-9.2.4-1-windows-x64-binaries.zip最新版待用,下列步骤达到手工安装好PostgreSQL,初始化数据... 查看详情

postgresql解压版怎么配置

postgresql解压版配置方法:改两个文件:postgresql.confpg_hba.conf-----------------比如环境中dbserverip为10.10.3.5, 客户端的ip为10.10.3.5在postgresql.conf文件添加:listen_addresses=\'*\'在pg_hba.conf文件添加:hostallall10.10.3.1... 查看详情

如何在 Windows 7 上使用命令 CMD 从 python django 访问和配置 postgresql 数据库

】如何在Windows7上使用命令CMD从pythondjango访问和配置postgresql数据库【英文标题】:HowtoaccessandconfigurepostgresqldatabasefrompythondjangoonWindows7usingacommandCMD【发布时间】:2019-07-0311:29:05【问题描述】:当我运行CMDWindows7命令时:pythonmanage.p... 查看详情

windows安装postgresql失败:therehasbeenanerror

Windows在安装PostgreSQL数据库的时候报错:Therehasbeenanerror,可能就是我的电脑名字为中文的原因。 试了好多个版本都报错这个,终于在今天得到了解决,如果你和我一样Windows的名字取名为中文的时候,在安装... 查看详情

PostgreSQL 未显示在 phpconfig() 函数上(在 Windows 上)

】PostgreSQL未显示在phpconfig()函数上(在Windows上)【英文标题】:PostgreSQLnotshowinguponphpconfig()function(onwindows)【发布时间】:2010-10-2615:38:22【问题描述】:我刚刚安装了Apache2.2.11并下载了PHP5.2.9-2的zip我没有评论extension=php_pdo_pgsql.dll... 查看详情

postgresql9.3版本安装好!pgadmin连接本机postgres数据库连接失败。。

下面是刚刚安装好postgres数据库完成向导后!点开开始菜单运行SQLShell(psql),在图片的4个参数请问需要填什么吗?希望高手指点下面是运行安装数据库时自动安装的pgadmin工具连接数据库发生的错误,请postgres高手指教。。我也遇... 查看详情

windows8专业版可以安装postgresql数据库吗?

操作系统是64位,postgresql-9.2.4-1-windows-x64数据库版本为最新版,管理员身份运行就报错。Windows8专业版可以安装PostgreSQL数据库。PostgreSQL安装:一、windows下安装过程安装介质:postgresql-9.1.3-1-windows.exe(46M),安装过程非常简单,... 查看详情

Windows 安全弹出窗口出现在主应用程序窗口后面

】Windows安全弹出窗口出现在主应用程序窗口后面【英文标题】:Windowssecuritypopupappearsbehindthemainapplicationwindow【发布时间】:2019-05-2506:25:48【问题描述】:我在WPF应用程序中使用Windows10API“Passport”和“WindowHello”。boolkeyCredentialAv... 查看详情

如何安装和使用wine,以便在linux上运行windows应用程序

...39;hasnoinstallationcandidateroot@localhost:/home/android#在Linux上运行Windows程序需要安装Wine,Wine的安装方法是用apt-get(Ubuntu、Debian类的系统)或者yum命令(CentOS、老版的Fedora)在线安装,注意这两种命令都需要用root用户身份来运行:apt-get... 查看详情

初识git

...能在Linux和Unix系统上跑。不过,慢慢地有人把它移植到了Windows上。现在,Git可以在Linux、Unix、Mac和Windows这几大平台上正常运行了。要使用Git,第一步当然是安装Git了。根据你当前使用的平台来阅读下面的文字:1.1在Linux上安装Gi... 查看详情