【PostgreSQL】安装和常用命令教程

news/2024/7/9 23:12:04 标签: postgresql, 数据库

PostgreSQL window安装教程

window安装PostgreSQL

建表语句:

DROP TABLE IF EXISTS student;
CREATE TABLE student (
  id serial NOT NULL,
  name varchar(100) NOT NULL,
  sex varchar(5) NOT NULL,
  PRIMARY KEY (id)
);

INSERT INTO student (id, name, sex) VALUES (1, '小明', '男');
INSERT INTO student (id, name, sex) VALUES (2, '小红', '女');

CREATE TABLE IF NOT EXISTS score
(
    id integer NOT NULL,
    student_id integer NOT NULL,
    grade integer NOT NULL,
    PRIMARY KEY (id)
);

insert into score(id,student_id,grade)values(1,1,80);
insert into score(id,student_id,grade)values(2,2,90);
PostgreSQL自增
MySQL 是用 AUTO_INCREMENT 这个属性来标识字段的自增。

PostgreSQL 使用序列来标识字段的自增长:

CREATE TABLE runoob
(
    id serial NOT NULL,
    alttext text,
    imgurl text
)

https://www.runoob.com/postgresql/postgresql-autoincrement.html

PostgreSQL表的创建

PostgreSQL表的创建

PostgreSQL linux安装教程1——centos6

linux安装PG教程

(1)安装依赖包

yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake

(2)下载PG安装包

http://www.postgresql.org/ftp/source/
在这里插入图片描述

(3)解压安装

在根目录下新建pgsql文件夹,并将pgsql的压缩包移入

tar -zxvf postgresql-15.3.tar.gz

进入解压后的文件夹,编译postgresql源码

cd postgresql-15.3/

./configure --prefix=/pgsql/postgresql

make:

make

make install

进入/pgsql/postgresql目录可以看到安装后的postgresql的文件。

在这里插入图片描述

(5)创建用户组postgres并创建用户postgres

groupadd postgres

useradd -g postgres postgres

id postgres

结果:

uid=501(postgres) gid=501(postgres) 组=501(postgres)

(6)创建postgresql数据库的数据主目录并修改文件所有者

cd /pgsql/postgresql

mkdir data

chown postgres:postgres data

ls -al

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

(7)配置环境变量

进入home/postgres目录可以看到.bash_profile文件。

cd /home/postgres

ls -al

在这里插入图片描述

修改:

vim .bash_profile

使其生效

source .bash_profile 

(8)切换用户到postgres并使用initdb初使用化数据库

su - postgres

initdb

可以看到 /pgsql/postgresql/data已经有文件:

cd /pgsql/postgresql/data

ls

在这里插入图片描述

(9)配置服务

修改/pgsql/postgresql/data目录下的两个文件。

vim postgresql.conf
listen_addresses = '*'

把这个地址改为*,表示在本地的所有地址上监听

vim pg_hba.conf

在# IPv4 local connections:下添加:

host    all             all             0.0.0.0/0                  trust

(10)设置PostgreSQL开机自启动

PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。

cd /root/pgsql/postgresql-15.3/contrib/start-scripts

在这里插入图片描述

添加执行权限:

chmod a+x linux

复制linux文件到/etc/init.d目录下,更名为postgresql

cp linux /etc/init.d/postgresql

修改/etc/init.d/postgresql文件的两个变量:

# Installation prefix
prefix=/pgsql/postgresql

# Data directory
PGDATA="/pgsql/postgresql/data"

设置postgresql服务开机自启动

chkconfig --add postgresql

查看:chkconfig

在这里插入图片描述

编辑/etc/sysconfig/iptables文件开放5432端口:

cd /etc/sysconfig

vim iptables

添加下列内容:

-A INPUT -p tcp -m tcp --dport 5432 -j ACCEPT

重启服务,或者其它命令:

systemctl restart iptables.service

查看端口是否开放:

/sbin/iptables -L -n

执行service postgresql start,启动PostgreSQL服务:

service postgresql start

在这里插入图片描述

查看PostgreSQL服务:

ps -ef | grep postgres

如果没有iptables:

systemctl stop firewalld.service && systemctl disable firewalld.service

yum install iptables-services iptables-devel

systemctl enable iptables.service && systemctl start iptables.service
查看状态 systemctl status iptables.service

查看iptables配置文件 vim /etc/sysconfig/iptables

psql失败:

psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: Connection refused
	Is the server running locally and accepting connections on that socket?
tail -f /pgsql/postgresql/data/pg_log/postgresql-*.log

netstat -nlp | grep 5432后没有任何信息,说明postgresql服务没有顺利启动。

PostgreSQL linux安装教程2——centos7

centos7安装PG

centos7安装PG2

centos7安装PG3

(1)进入官方下载https://www.postgresql.org/download/

选择对应的系统和对应的版本,注意,版本不要太高:

image-20230613170415723

PG11安装命令:

# Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

# Install PostgreSQL:
sudo yum install -y postgresql11-server

# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-11/bin/postgresql-11-setup initdb
sudo systemctl enable postgresql-11
sudo systemctl start postgresql-11

创建数据库测试:

切换到postgres用户

su - postgres
psql 

创建用户

create user test with password '123.com';

创建数据库

create database test_db owner test;

退出

\q

(2)配置远程连接
修改文件postgresql.conf :

vim /var/lib/pgsql/11/data/postgresql.conf

添加:

listen_addresses = ‘*’

修改pg_hba.conf文件:

vim /var/lib/pgsql/11/data/pg_hba.conf

添加:

host all all 0.0.0.0/0 trust

(3)重启并关闭防火墙

重启服务:

systemctl restart postgresql-11.service

关闭防火墙:

systemctl stop firewalld

注意,

  • 如果是腾讯云/阿里云,需要配置端口开放,很多时候卡在这。
  • 防火墙没关闭完全
  • 连接软件全都关闭试试

(4)验证

可以通过navicat来尝试能否连接hsang

PG常用指令

PG数据库教程

查看数据库
\l
使用数据库
\c imdbload
查看表
\dt
查看索引
\di
执行sql脚本
\i /var/lib/pgsql/benchmark/schematext.sql;
生成执行计划
EXPLAIN (FORMAT JSON, analyze)
SELECT MIN(mc.note) AS production_note,
       MIN(t.title) AS movie_title,
       MIN(t.production_year) AS movie_year
FROM company_type AS ct,
     info_type AS it,
     movie_companies AS mc,
     movie_info_idx AS mi_idx,
     title AS t
WHERE ct.kind = 'production companies'
  AND it.info = 'top 250 rank'
  AND mc.note NOT LIKE '%(as Metro-Goldwyn-Mayer Pictures)%'
  AND (mc.note LIKE '%(co-production)%'
       OR mc.note LIKE '%(presents)%')
  AND ct.id = mc.company_type_id
  AND t.id = mc.movie_id
  AND t.id = mi_idx.movie_id
  AND mc.movie_id = mi_idx.movie_id
  AND it.id = mi_idx.info_type_id;
创建物化视图
CREATE MATERIALIZED VIEW mv2 AS
select info_type.id AS id, info_type.info AS info, movie_info_idx.movie_id AS movie_id
 from movie_info_idx,info_type
 where (movie_info_idx.info_type_id = info_type.id) And (info_type.info = 'top 250 rank');
EXPLAIN (FORMAT JSON, analyze)
select info_type.id AS id, info_type.info AS info, movie_info_idx.movie_id AS movie_id
 from movie_info_idx,info_type
 where (movie_info_idx.info_type_id = info_type.id) And (info_type.info = 'top 250 rank');
EXPLAIN (FORMAT JSON, analyze)
SELECT MIN(t.title) AS movie_title
FROM company_name AS cn,
     movie_companies AS mc,
     title AS t,
     mv12
WHERE cn.country_code ='[us]'
  AND cn.id = mc.company_id
  AND mc.movie_id = t.id
  AND t.id = mv12.movie_id
  AND mc.movie_id = mv12.movie_id;







postgreSQL取某个表的前100行作为一个新表
CREATE TABLE new_table AS
SELECT * FROM original_table
LIMIT 100;
暂停PG运行
sudo systemctl stop postgresql-11
设置PG并行执行相关
set max_parallel_workers_per_gather = 0;

表示禁用每个 Gather 操作的并行执行。

如果您需要禁用全局并行查询,您还需要将 max_parallel_workers 参数设置为 0

关闭Memoize算子
set enable_memoize TO off;
修改PG参数文件

14是PG的版本名字

/var/lib/pgsql/14/data

可修改参数:

(1)work_mem

在 PostgreSQL 中,work_mem 参数控制每个工作进程使用的内存量。该参数设置得越高,可以执行更复杂的查询,但是也将占用更多的系统资源和内存。

work_mem = 2GB

(2)shared_buffers

请注意,shared_buffers 参数控制着 PostgreSQL 使用的共享内存缓冲区的大小。设置得太小可能导致频繁的磁盘读写操作,而设置得太大可能会占用过多的系统内存。建议根据您的硬件配置和数据库负载进行适当的调整。

shared_buffers = 1GB

(3)effective_cache_size

请注意,effective_cache_size 参数用于告诉 PostgreSQL 系统可用的内存缓存大小。设置得太小可能导致频繁的磁盘读写操作,而设置得太大可能会占用过多的系统内存。建议根据您的硬件配置和数据库负载进行适当的调整。

effective_cache_size = 32GB

(4)gego_threshold

gego_threshold = 18
查看配置文件路径
show config_file;

image-20231018164654113

修改PG的data存储路径

https://www.cnblogs.com/miracle-luna/p/17019256.html

em` 参数控制每个工作进程使用的内存量。该参数设置得越高,可以执行更复杂的查询,但是也将占用更多的系统资源和内存。

work_mem = 2GB

(2)shared_buffers

请注意,shared_buffers 参数控制着 PostgreSQL 使用的共享内存缓冲区的大小。设置得太小可能导致频繁的磁盘读写操作,而设置得太大可能会占用过多的系统内存。建议根据您的硬件配置和数据库负载进行适当的调整。

shared_buffers = 1GB

(3)effective_cache_size

请注意,effective_cache_size 参数用于告诉 PostgreSQL 系统可用的内存缓存大小。设置得太小可能导致频繁的磁盘读写操作,而设置得太大可能会占用过多的系统内存。建议根据您的硬件配置和数据库负载进行适当的调整。

effective_cache_size = 32GB

(4)gego_threshold

gego_threshold = 18
查看配置文件路径
show config_file;

[外链图片转存中…(img-ojLvcnHW-1705414404781)]

修改PG的data存储路径

https://www.cnblogs.com/miracle-luna/p/17019256.html


http://www.niftyadmin.cn/n/5331719.html

相关文章

rust获取本地外网ip地址的方法

大家好,我是get_local_info作者带剑书生,这里用一篇文章讲解get_local_info的使用。 get_local_info是什么? get_local_info是一个获取linux系统信息的rust三方库,并提供一些常用功能,目前版本0.2.4。详细介绍地址&a…

第三章 计算机网络技术基础——教案(附PPT)

第三章 计算机网络技术基础 一、教学目标: 1. 掌握几种常见网络拓扑结构的原理及其特点 2. 掌握ISO/OSI网络参考模型及各层的主要功能 3. 掌握共享介质方式的CSMA/CD和令牌传递两种数据传输控制方式的基本原理 4. 了解几种常见的网络类型 5. 掌握TCP/IP协议的层次结构及…

【项目经验】详解Puppeteer入门及案例

文章目录 一.项目需求及Puppeteer是什么?二.Puppeteer注意事项及常用的方法1.注意事项2.常用的方法*puppeteer.launch()**browser.newPage()**page.goto()**page.on(request,()> {})**page.e…

数据可视化|Python之Pyecharts将“爬虫数据”绘制饼状图

前言 本文是该专栏的第40篇,后面会持续分享python数据分析的干货知识,记得关注。 在项目中,可能有些同学或多或少遇见这样的需求。将爬虫采集下来的数据,进行图像可视化处理,方便其他业务线进行数据分析处理。 而本文,笔者将以某个爬虫案例的采集数据为例子,使用Pytho…

SpringBoot项目的两种发布方式(jar包和war包)

SpringBoot项目的两种发布方式&#xff08;jar包和war包&#xff09; 在springboot入门和项目示例这个项目和application.yml配置端口号和访问url路径基础上进行修改 1、使用jar包方式发布 1.1、在pom.xml中添加一个SpringBoot的构建的插件 <build><plugins>&l…

【征服redis4】一文征服redis的Lettuce客户端

前面我们介绍了jedis客户端的使用&#xff0c;本文我们继续介绍第二个常用的客户端Lettuce。与jedis相比Lettuce有什么特征和区别&#xff0c;以及使用的时候有哪些重要的问题呢&#xff1f;本文我们一起来探究一下。 1. Lettuce介绍与安装测试 1.1. 什么是Lettuce Lettuce是…

c++八股8

友元函数能不能是虚函数&#xff1a; 友元函数不属于类的成员函数&#xff0c;它独立于类存在&#xff0c;因此不存在是否为虚函数的概念。友元函数主要用于突破类的封装性&#xff0c;使外部函数能访问类的私有和保护成员&#xff0c;它与类的继承和多态无关。TCP三握四挥&…

爬虫补环境jsdom、proxy、Selenium案例:某条

声明&#xff1a; 该文章为学习使用&#xff0c;严禁用于商业用途和非法用途&#xff0c;违者后果自负&#xff0c;由此产生的一切后果均与作者无关 一、简介 爬虫逆向补环境的目的是为了模拟正常用户的行为&#xff0c;使爬虫看起来更像是一个真实的用户在浏览网站。这样可以…