Linux上安装postgres 10.5

由于接触了华为的elk大数据平台,里面封装的是postgres ,就想着安装一下,熟悉一下postgres数据。

安装包下载:https://www.postgresql.org/ftp/source/    

可以选择自己想下载的版本。这里我们下载v10.5

随后开始安装:

1.把包上传到虚拟机中postgresql-10.5.tar.gz

2.解压缩:

gunzip postgresql-10.5.tar.gz  

tar -xvf postgresql-10.5.tar

3.进行编辑安装:

./configure         --预编辑
make
su                       --进root用户
make install       --安装
adduser postgres               --创建postgres
mkdir /usr/local/pgsql/data   --创建目录
chown postgres /usr/local/pgsql/data   
su - postgres
/usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data   --初始化数据库

/usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data >logfile 2>&1 &        --启动数据库

遇到错误:
[postgres@master ~]$ more logfile 
2018-12-20 15:47:11.867 CST [28074] LOG:  listening on IPv6 address "::1", port 5432
2018-12-20 15:47:11.867 CST [28074] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2018-12-20 15:47:11.868 CST [28074] FATAL:  could not remove old lock file "/tmp/.s.PGSQL.5432.lock": 不允许的操作
2018-12-20 15:47:11.868 CST [28074] HINT:  The file seems accidentally left over, but it could not be removed. Please remove the file by hand and try again.
2018-12-20 15:47:11.868 CST [28074] LOG:  database system is shut down

解决方法:
chown -R postgres:postgres /tmp             --root用户

/usr/local/pgsql/bin/createdb test             --创建test数据库
/usr/local/pgsql/bin/psql test                    --进入test数据库

[postgres@master bin]$ /usr/local/pgsql/bin/psql test      
psql (10.5)
Type "help" for help.

test=# 

启动或重启server
先确保是切换到了/pgsql/bin目录下,并且切换Linux用户postgres
cd /usr/local/pgsql/bin/
su – postgres
启动server:
./pg_ctl start -D /usr/local/pgsql/data

重启:
./pg_ctl restart -D /usr/local/pgsql/data 

waiting for server to shut down....2018-12-20 16:34:51.304 CST [28748] LOG:  received fast shutdown request
2018-12-20 16:34:51.305 CST [28748] LOG:  aborting any active transactions
2018-12-20 16:34:51.306 CST [28944] FATAL:  terminating connection due to administrator command
2018-12-20 16:34:51.307 CST [28945] FATAL:  terminating connection due to administrator command
2018-12-20 16:34:51.307 CST [28942] FATAL:  terminating connection due to administrator command
2018-12-20 16:34:51.308 CST [28941] FATAL:  terminating connection due to administrator command
2018-12-20 16:34:51.310 CST [28748] LOG:  worker process: logical replication launcher (PID 28755) exited with exit code 1
2018-12-20 16:34:51.312 CST [28750] LOG:  shutting down
2018-12-20 16:34:51.327 CST [28748] LOG:  database system is shut down
 done
server stopped
waiting for server to start....2018-12-20 16:34:51.413 CST [29138] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2018-12-20 16:34:51.413 CST [29138] LOG:  listening on IPv6 address "::", port 5432
2018-12-20 16:34:51.415 CST [29138] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2018-12-20 16:34:51.438 CST [29139] LOG:  database system was shut down at 2018-12-20 16:34:51 CST
2018-12-20 16:34:51.442 CST [29138] LOG:  database system is ready to accept connections
 done
server started

新建数据库和可以登录数据库的用户密码
确保是在/usr/local/pgsql/bin/目录下
./createdb mydb

创建用户(如用户名为lin,密码为LinBug)有两种方式:

第一种:
CREATE USER或CREATE ROLE:CREATE USER是CREATE ROLE的一个别名。
唯一的区别是CREATE USER命令缺省是LOGIN,而CREATE ROLE命令缺省是NOLOGIN。
先进入默认的postgres数据库:
./psql
然后执行:
CREATE USER zhang WITH PASSWORD 'zhang';
创建成功提示如下
[postgres@master bin]$ ./psql
psql (10.5)
Type "help" for help.

postgres=# CREATE USER zhang WITH PASSWORD 'zhang';
CREATE ROLE
postgres=#
第二种:
 pg封装的命令方式:
[postgres@master bin]$ ./createuser -P zhang
Enter password for new role:
Enter it again:
2018-12-20 16:04:01.792 CST [28674] ERROR:  role "zhang" already exists
2018-12-20 16:04:01.792 CST [28674] STATEMENT:  CREATE ROLE zhang PASSWORD 'md578d7c325e6ea326d9c1a5c5b1172a59d' NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT LOGIN;
createuser: creation of new role failed: ERROR:  role "zhang" already exists

访问数据库

* 确保在/usr/local/pgsql/bin/目录下,
以默认用户名访问默认数据库(默认的用户名和数据库名都是postgres):
./psql
于是进入PG的交互终端psql
[postgres@master bin]$ ./psql
psql (10.5)
Type "help" for help.

postgres=#

以名为zhang的角色登录名为mydb的数据库:

./psql mydb -U zhang
可以看出,当psql终端的提示符为=#时,表示当前登录的是超级用户,而当提示符为=>时则为普通用户
[postgres@master bin]$ ./psql mydb -U zhang
psql (10.5)
Type "help" for help.

mydb=>

远程访问数据库设置
* 远程访问数据库的认证方式主要有很多方式,我只设置基于TCP/IP连接的trust认证方式
需设置两个配置文件,
1)修改配置文件postgresql.conf,
vim /usr/local/pgsql/data/postgresql.conf
修改监听地址:
#listen_addresses=’localhost’
#将上面这行改成如下
listen_addresses=’*’

修改配置文件/pgsql/data/pg_hba.conf:
vim /usr/local/pgsql/data/pg_hba.conf
添加一条IP授权记录(如192.168.2.23),可以对一个网段授权
# 这是在/pgsql/data/pg_hba.conf文件里加
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
host    all             all             0.0.0.0/0               trust

设置完需要重启数据库才能生效。

远程客户端连接

使用的软件为:Dbeaver

主要的就是以上配置。

现在可以在客户端操作数据库了。

<<完>>

原文地址:https://www.cnblogs.com/hello-wei/p/10150322.html