centos7安装postgresql8.2.15及存储过程创建

centos7安装postgresql8.2.15及存储过程创建,第1张

centos7安装postgresql8.2.15
  • 下载
  • 创建用户安装部署
    • 创建安装用户pgsql
    • 赋予sudo权限
    • 下载、解压、安装、配置
  • 数据库初始化、启动
    • 初始化报错
    • 启动
    • 停止
  • 创建数据库
    • 修改配置,尝试连接数据库
  • 远程连接数据库
  • 总结
  • 20220429-更新
    • 创建用户密码、数据库
    • 创建存储过程
      • 报错

下载

下载地址

创建用户安装部署 创建安装用户pgsql

非必要步骤,可以直接用root用户

[root@host1 ~]# useradd pgsql -d /home/pgsql
[root@host1 ~]# passwd pgsql
更改用户 pgsql 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@host1 ~]# su - pgsql
[pgsql@host1 ~]$ 
[pgsql@host1 ~]$ 
赋予sudo权限

需要root权限执行下载安装 *** 作

[pgsql@host1 ~]$ 
[pgsql@host1 ~]$ 登出
[root@host1 ~]# echo 'pgsql ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers
[root@host1 ~]# su - pgsql
上一次登录:三 4月 27 20:20:16 CST 2022pts/0 上
[pgsql@host1 ~]$ sudo -v
[pgsql@host1 ~]$ 
下载、解压、安装、配置

下载

wget https://www.postgresql.org/ftp/source/v8.2.15/postgresql-8.2.15.tar.gz --no-check-certificate



网速太差,只能慢慢页面下载好,丢到服务器

解压

[pgsql@host1 ~]$ ll
总用量 15404
-rw-r--r--. 1 pgsql pgsql 15772131 4月  27 20:52 postgresql-8.2.15.tar.gz
[pgsql@host1 ~]$ 
[pgsql@host1 ~]$ tar xf postgresql-8.2.15.tar.gz 
[pgsql@host1 ~]$ cd postgresql-8.2.15
[pgsql@host1 postgresql-8.2.15]$ mkdir -p /home/pgsql/postgresql

安装依赖

sudo  yum install  -y readline-devel  zlib-devel  

编译

[pgsql@host1 postgresql-8.2.15]$ ./configure --prefix=/home/pgsql/postgresql


安装

[pgsql@host1 postgresql-8.2.15]$ make && make install

配置环境变量

export PATH=$PATH:/home/pgsql/postgresql/bin
export PGDATA=/home/pgsql/data/pgdata

创建data目录

[pgsql@host1 postgresql]$ cd bin
[pgsql@host1 bin]$ pwd
/home/pgsql/postgresql/bin
[pgsql@host1 bin]$ cd
[pgsql@host1 ~]$ mkdir -p data/pgdata
[pgsql@host1 ~]$ chmod 0700 -R data
[pgsql@host1 ~]$ vi .bash_profile 
[pgsql@host1 ~]$ . .bash_profile 

![在这里插入图片描述](http://www.kaotop.com/file/tupian/20220506/0d59d78e69c9472bbc3b5b9647460755.png

数据库初始化、启动

初始化命令

 initdb  /home/pgsql/data/pgdata
初始化报错
[pgsql@host1 ~]$ initdb  /home/pgsql/data/pgdata
The files belonging to this database system will be owned by user "pgsql".
This user must also own the server process.

The database cluster will be initialized with locale zh_CN.UTF-8.
The default database encoding has accordingly been set to UTF8.

fixing permissions on existing directory /home/pgsql/data/pgdata ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers/max_fsm_pages ... 32MB/204800
creating configuration files ... ok
creating template1 database in /home/pgsql/data/pgdata/base/1 ... ok
initializing pg_authid ... FATAL:  wrong number of index expressions
STATEMENT:  CREATE TRIGGER pg_sync_pg_database   AFTER INSERT OR UPDATE OR DELETE ON pg_database   FOR EACH STATEMENT EXECUTE PROCEDURE flatfile_update_trigger();
	
child process exited with exit code 1
initdb: removing contents of data directory "/home/pgsql/data/pgdata"
[pgsql@host1 ~]$ 

原因:数据库版本太低,环境GCC版本高所致
数据库不能升级的情况下,编译安装的时候加个参数

cd postgresql-8.2.15
make clean
./configure --prefix=/home/pgsql/postgresql CFLAGS="-Wno-aggressive-loop-optimizations"
make && make install 


验证,初始化成功

postgresql 配置(postgresql.conf)

[pgsql@host1 ~]$ ll
总用量 15408
drwxrwxr-x. 3 pgsql pgsql       20 4月  27 21:19 data
-rw-------. 1 pgsql pgsql      113 4月  27 21:25 logfile
drwxrwxr-x. 8 pgsql pgsql       78 4月  27 21:15 postgresql
drwxrwxr-x. 6 pgsql pgsql      269 4月  27 21:08 postgresql-8.2.15
-rw-r--r--. 1 pgsql pgsql 15772131 4月  27 20:52 postgresql-8.2.15.tar.gz
[pgsql@host1 ~]$ 
[pgsql@host1 ~]$ cd data/pgdata/
[pgsql@host1 pgdata]$ vi postgresql.conf 

启动

停止

[pgsql@host1 ~]$ pg_ctl -D /home/pgsql/data/pgdata -l logfile start smart

-m指定数据库的停止方法,有以下三种
smart:等所有的连接中止后,关闭数据库。如果客户端不中止,则无法关闭数据库。
fast:快速关闭数据库,断开客户端的连接,让已有的事务回滚,然后正常关闭数据库。
immediate:立即关闭数据库,相当于数据库进程立即停止,直接退出,下次启动数据库需要进行修复。

创建数据库

创建数据库、用户,连接数据库

[pgsql@host1 bin]$ pwd
/home/pgsql/postgresql/bin
[pgsql@host1 bin]$ # 创建数据库
[pgsql@host1 bin]$ ./createdb dolphinscheduler
CREATE DATABASE
[pgsql@host1 bin]$ # 创建用户
[pgsql@host1 bin]$ ./createuser dolphin_user
Shall the new role be a superuser? (y/n) y
CREATE ROLE
[pgsql@host1 bin]$ # 连接数据库
[pgsql@host1 bin]$ ./psql -h 127.0.0.1 -p 5432 -U dolphin_user -d dolphinscheduler
Welcome to psql 8.2.15, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

dolphinscheduler=# \q
[pgsql@host1 bin]$ ./psql -h 192.168.56.10 -p 5432 -U dolphin_user -d dolphinscheduler
psql: could not connect to server: 拒绝连接
	Is the server running on host "192.168.56.10" and accepting
	TCP/IP connections on port 5432?
[pgsql@host1 bin]$ 
修改配置,尝试连接数据库

pg_hba.conf 添加192.168.56.10

[pgsql@host1 pgdata]$ vi pg_hba.conf 
[pgsql@host1 pgdata]$ cat pg_hba.conf |grep -v "#"
local   all         all                               trust
host    all         all         127.0.0.1/32          trust
host    all         all         192.168.56.10/32          trust
host    all         all         ::1/128               trust
[pgsql@host1 pgdata]$ pwd
/home/pgsql/data/pgdata
[pgsql@host1 pgdata]$ 

重启数据库

[pgsql@host1 pgdata]$ pg_ctl -D /home/pgsql/data/pgdata -l logfile stop
waiting for server to shut down.... done
server stopped
[pgsql@host1 pgdata]$ pg_ctl -D /home/pgsql/data/pgdata -l logfile start
server starting

再次验证

./psql -h 192.168.56.10 -p 5432 -U dolphin_user -d dolphinscheduler

远程连接数据库


总结
  postgres是安装之后产生的默认超级用户。
  对于pgsql,基本上就是小白,此次安装pgsql纯粹为了后面测试海豚调度的pgsql存储过程使用。今晚先到这!
20220429-更新

海豚调度数据源中心配置数据源,需要用户密码,创建用户、数据库命令如下

创建用户密码、数据库
[postgres@devhost02 ~]$ /usr/local/postgresql/bin/psql -h 192.168.3.11 -p 5432 -U postgres -d postgres
Welcome to psql 8.2.15, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

postgres=# CREATE USER testuser WITH PASSWORD 'testuser';
CREATE ROLE
postgres=# CREATE DATABASE test_db OWNER testuser;
CREATE DATABASE
postgres=#


创建存储过程 报错
> ERROR:  language "plpgsql" does not exist
HINT:  Use CREATE LANGUAGE to load the language into the database.


解决

只有超级用户才能创建language

[postgres@devhost02 ~]$ /usr/local/postgresql/bin/psql -h 192.168.3.11 -p 5432 -U postgres -d test_db
Welcome to psql 8.2.15, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit

postgres=# CREATE LANGUAGE 'plpgsql';
CREATE LANGUAGE
postgres=# 

通过终端工具navicat执行依然报错,但是通过Linux终端执行成功!

CREATE OR REPLACE FUNCTION "public"."p_test_pgsql"(IN "i_date" varchar, OUT "o_return_code" int4, OUT "o_return_msg" varchar)
  RETURNS "pg_catalog"."record" AS $BODY$
declare
 vs_return_msg VARCHAR;
begin
   O_RETURN_CODE = 0;
   O_RETURN_MSG = i_date || ' 成功';
   RETURN;
end;
$BODY$
  LANGUAGE plpgsql VOLATILE

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/800497.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-07
下一篇 2022-05-07

发表评论

登录后才能评论

评论列表(0条)

保存