标签归档:PostgreSQL

Windows安装PostgreSQL

需要管理身份运行powershell

# 初始化数据库
D:\server\PostgreSQL\15\bin\pg_ctl.exe init 
# 开启服务
D:\server\PostgreSQL\15\bin\pg_ctl.exe start -N "postgres_15" -D "D:\server\PostgreSQL\15\data"
# 关闭服务
D:\server\PostgreSQL\15\bin\pg_ctl.exe stop -N "postgres_15" -D "D:\server\PostgreSQL\15\data"
# 注册windows系统服务并设置自动启动
D:\server\PostgreSQL\15\bin\pg_ctl.exe register -N "postgres_15" -D "D:\server\PostgreSQL\15\data" -S "auto"

运行postgres_15服务,默认本地连接时不需要密码的。

密码等设置在D:\server\PostgreSQL\15\data\pg_hba.conf

宝塔Postgresql扩展安装

以pg_stat_statements为例

编译扩展

cd /usr/local/pgsql/contrib/pg_stat_statements/
make && make install

启用扩展

切换为postgres用户sudo su - postgres,运行sql命令行psql,启用扩展

CREATE EXTENSION pg_stat_statements;

修改postgresql.conf

进入宝塔面板,修改postgres配置

shared_preload_libraries= 'pg_stat_statements'

重启postgresql

PostgreSQL之json字符字段查找

根据字段值查找

SELECT t.*
FROM table_name t
WHERE json_field->>'type'='5' and  json_field->>'value'!=''

查找并替换

update
table_name d
set cover_content=(cover_content::jsonb || jsonb_build_object('cover_img_id',((select f.id  from file f WHERE  f.path = SUBSTRING(cover_content->>'value',44))  limit 1)::text)::jsonb)
where cover_content->>'type'='5' and  cover_content->>'value'!='' and cover_content->>'cover_img_id' is null;

更新嵌套对象的键值:

如果 jsonb 字段包含嵌套对象,例如:

'{"user": {"name": "Alice", "age": 30}}'

你想更新 user 对象中的 age 为 35,可以使用:

UPDATE your_table
SET jsonb_column = jsonb_set(jsonb_column, '{user,age}', '"35"');

数组里面的字段查找

字段里面存的是数组,数组里面存的对象,可以通过以下语句直接查询对象属性的值

select *
    from cart,
jsonb_array_elements(data::jsonb) as elem where ( elem->>'cover_img_id' is null or  elem->>'cover_img_id'='') order by created_at desc;

数组字段查找并替换

update cart set data= (
  SELECT jsonb_agg(
    CASE
      WHEN element->>'cover_img_url' <> '' THEN jsonb_set(element, '{cover_img_id}',
          ((((select f.id  from file f WHERE  f.path = SUBSTRING(element->>'cover_img_url',44))  limit 1)::text)::jsonb)
          )
      ELSE element
    END
  )
  FROM jsonb_array_elements(data::jsonb) AS element
)
where id in (
    select id
    from cart,
jsonb_array_elements(data::jsonb) as elem where elem->>'cover_img_url' <> '' and ( elem->>'cover_img_id' is null or  elem->>'cover_img_id'='') order by created_at desc
    );

多级数组查找替换

select id                                as cart_id,
       cart_user_id                      as user_id,
       elem ->> 'id'           as file_id,
       (updated_at + interval '30 days') as expired_at
from cart,
     jsonb_array_elements(data::jsonb) as elem
limit 10;

update file f
set expired_at=t.expired_at
from (select id                                as cart_id,
       cart_user_id                      as user_id,
       (elem ->> 'id')::int           as file_id,
       (updated_at + interval '30 days') as expired_at
from cart,
     jsonb_array_elements(data::jsonb) as elem)
         as t
where f.id = t.file_id
  and f.expired_at < t.expired_at;

使用 WSL 作为开发环境 (Ubuntu+PostgreSQL+MariaDB/Mysql+PHP+Python+NodeJS+镜像安装)

安装后配置

修改DNS

/etc/resolv.conf

nameserver 223.5.5.5

修改软件源

一般情况下,将 /etc/apt/sources.list 文件中 Ubuntu 默认的源地址 http://archive.ubuntu.com/ 替换为 http://mirrors.ustc.edu.cn 即可。

可以使用如下命令:

sudo sed -i ‘s/archive.ubuntu.com/mirrors.ustc.edu.cn/g’ /etc/apt/sources.list

更新包

sudo apt update
sudo apt upgrade

PostgreSQL

新增apt源配置

新增配置文件 /etc/apt/sources.list.d/pgdg.list,内容:

deb https://mirrors.ustc.edu.cn/postgresql/repos/apt/ focal-pgdg main

bionic可以换成去其他版本号

导入key并安装

wget --quiet -O - https://mirrors.ustc.edu.cn/postgresql/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt install postgresql-12
sudo systemctl enable postgresql
sudo service start postgresql

创建数据库以及账户

切换到postgres用户并运行psql

sudo -u postgres psql

创建数据以及用户账户

CREATE USER c4ys WITH PASSWORD 'c4ys';
CREATE DATABASE c4ys OWNER c4ys;

MariaDb

下载key

sudo apt-get install software-properties-common
sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] https://mirrors.ustc.edu.cn/mariadb/repo/10.5/ubuntu focal main'
sudo apt update
sudo apt install mariadb-server

将源配置修改到单独文件

系统会自动在source.list增加配置,可以将配置移动到单独文件

/etc/apt/sources.list.d/MariaDB.list

deb [arch=ppc64el,arm64,amd64] https://mirrors.ustc.edu.cn/mariadb/repo/10.5/ubuntu focal main
# deb-src [arch=ppc64el,arm64,amd64] https://mirrors.ustc.edu.cn/mariadb/repo/10.5/ubuntu focal main

自动启动

sudo systemctl enable mariadb
sudo service mariadb start
sudo mysql_secure_installation

Python

将python3作为默认Python

sudo apt install python-is-python3  python3-pip

PHP+NGINX

sudo apt install nginx nginx php-cli php-dev php-mbstring php-mbstring  php-intl php-xml php-redis php-gd php-fpm php-curl php-bcmath php-zip -y

NPM+NGINX

sudo apt install npm

参考

postgresql 常用命令

创建用户

CREATE USER davide WITH PASSWORD 'jw8s0F4';
CREATE ROLE admin WITH CREATEDB CREATEROLE;

修改用户密码

ALTER ROLE davide WITH PASSWORD 'hu8jmn3';
ALTER ROLE davide WITH PASSWORD NULL;

创建数据库

CREATE DATABASE sales OWNER salesapp

设置数据库只读权限

create user test_readonly with password 'password';
grant connect on DATABASE test to test_readonly;
grant select on all tables in schema public to test_readonly;

导出导入

pg_dump -F t -h 127.0.0.1 -U postgres dbname > dbname.tar
pg_restore -d dbname dbname.tar

CentOS7通过yum安装PostgreSQL10

发现最新的postgresql版本

https://www.postgresql.org/download/linux/redhat/

下载

yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-centos10-10-2.noarch.rpm

安装

yum install postgresql10-server

设置数据目录

创建数据目录并修改权限

mkdir -p /data/postgresql/
chown postgres:postgres /data/postgresql/

复制开机启动文件

sudo cp /usr/lib/systemd/system/postgresql-10.service /etc/systemd/system/

修改开机启动文件

vim /etc/systemd/system/postgresql-10.service

找到

Environment=PGDATA=/var/lib/pgsql/10/data/

修改为

Environment=PGDATA=/data/postgresql/

初始化数据库

/usr/pgsql-10/bin/postgresql-10-setup initdb

开机自启动

systemctl enable postgresql-10
systemctl start postgresql-10
systemctl status postgresql-10

修改本地帐户权限

vim /data/postgresql/pg_hba.conf

host    all             all             127.0.0.1/32            ident

修改为

host    all             all             127.0.0.1/32            md5

重新加载配置

systemctl restart postgresql-10

新增用户和数据库

su - postgres
createuser dbuser
createdb -e -O dbuser dbname

设定密码

su - postgres
psql
\password dbuser (输入两次密码)

新用户登录数据库

psql -U dbuser -d dbname -h 127.0.0.1 (输入之前的密码)

允许非本机ip登录

  • 编辑data/postgresql.conf修改listen_addresses = 'localhost'listen_addresses = 'localhost'
  • 编辑data/pg_hba.conf

    host all all 192.168.1.0/24 md5

fedora27(centos)下用过dnf(yum)安装postgresql10

安装postgresql10-server

所有repo包都在 https://yum.postgresql.org/下,这里通过一级一级,先是postgresql10版本,然后选择fedora 10,然后下载安装

dnf install https://download.postgresql.org/pub/repos/yum/10/fedora/fedora-27-x86_64/pgdg-fedora10-10-3.noarch.rpm dnf install postgresql10-server

安装 pgadmin4

dnf install pgadmin4-v2

初始化数据库

sudo /usr/pgsql-10/bin/postgresql-10-setup initdb

运行数据库

系统服务被安装在/usr/lib/systemd/system/postgresql-10.service

systemctl start postgresql-10
systemctl enable postgresql-10

修改本地登录无需密码

sudo vim /var/lib/pgsql/10/data/pg_hba.conf

#host    all             all             127.0.0.1/32            ident
host    all             all             127.0.0.1/32            trust