当前位置:   article > 正文

linux postgresql 远程访问,linux中postgresql安装使用

linux postgresql 远程访问,linux中postgresql安装使用

安装数据库

注:安装前执行以下命令

setenforce 0

1、通过postgresql下载地址,下载最新版本的postgresql。

2、执行以下命令安装数据库。

chmod +x PostgreSQL-*-linux-x64-installer.run

./PostgreSQL-*-linux-x64-installer.run

3、一路默认安装下去。

4、 postgresql的bin路径加入.bash_profile后刷新

/opt/2ndQuadrant/PostgreSQL/12/bin

source .bash_profile

5、创建数据库

createdb -U postgres [数据库名]

6、登录数据库

psql -U postgres

\connect [数据库名]

DROP SCHEMA public CASCADE;

\q

7、备份导入数据库文件

通过DBeaver选择customer导出数据库或使用如下命令行:

pg_dump.exe --format=c -n "uniauth" -n "public" --verbose --host=127.0.0.1 --port=5432 --username=postgres -f [备份名称].backup [数据库名]

恢复命令如下:

pg_restore --verbose --host=127.0.0.1 --port=5432 --username=postgres --format=c --dbname=[数据库名] -1 [备份名称].backup

开启远程访问

打开postgresql.conf文件,修改 listen_addresses:

listen_addresses = '*'

增加以下行到文件pg_hba.conf中:

host all all 203.0.113.2/32 md5

203.0.113.2/32是允许访问本机数据库的地址,如果允许所有地址访问则改为'all'或'0.0.0.0/0'。

9、查看启停方法

more /opt/2ndQuadrant/PostgreSQL/12/README-linux-x64.txt

systemctl stop postgresql-2ndQ-12

systemctl start postgresql-2ndQ-12

10、关闭防火墙

systemctl stop firewalld

systemctl disable firewalld

迁移postgres数据目录

当发现数据空间不足时,使用以下步骤迁移数据目录:

1)停止数据库

systemctl stop postgresql-2ndQ-12.service

2)迁移数据

sudo mv /opt/2ndQuadrant/PostgreSQL/12/data /home/postgres

3)备份/etc/systemd/system/postgresql-2ndQ-12.service,然后修改postgres服务启动数据路径

sudo chmod +w /etc/systemd/system/postgresql-2ndQ-12.service

sudo vi /etc/systemd/system/postgresql-2ndQ-12.service

4)修改服务中6处数据目录路径,如下图:

68dc464b92a1

image.png

修改结果如下:

68dc464b92a1

image.png

5)重新加载服务并启动数据库

systemctl daemon-reload

systemctl start postgresql-2ndQ-12.service

复制数据库

创建并复制命令

CREATE DATABASE newdb WITH TEMPLATE hxcloud OWNER postgres;

会报如下错误

68dc464b92a1

image.png

使用以下语句断开所有访问用户:

SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity

WHERE pg_stat_activity.datname = 'hxcloud' AND pid <> pg_backend_pid();

然后再执行创建复制数据库命令即可

常见错误

1、如果报llvmjit.so错误,在数据库里执行以下命令:

postgres=# show jit;

postgres=# alter system set jit=on;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/657142
推荐阅读
相关标签
  

闽ICP备14008679号