1 下载、安装及数据库初始化
下载安装postgresql官方yam仓库,使用yum install命令完成
复制
下载安装postgresql15-server
Error: Package: postgresql15-server-15.1-1PGDG.rhel7.x86\_64 (pgdg15) Requires: libzstd.so.1()(64bit) Error: Package: postgresql15-15.1-1PGDG.rhel7.x86\_64 (pgdg15) Requires: libzstd.so.1()(64bit) Error: Package: postgresql15-15.1-1PGDG.rhel7.x86\_64 (pgdg15) Requires: libzstd >= 1.4.0 You could try using --skip-broken to work around the problem You could try running: rpm -Va --nofiles --nodigest
复制
有一个依赖包(libzstd)需要手动安装安装以下,版本不能低于1.4.0
[root@localhost ~\]# rpm -ivh libzstd-1.5.2-1.el7.x86\_64.rpm
复制
再次安装postgresql15-server后,进行数据库初始化。
[root@localhost ~\]/usr/pgsql-15/bin/postgresql-15-setup initdb
复制
设置数据库开机自动启动后启动数据库
[root@localhost ~\]systemctl enable postgresql-15 [root@localhost ~\]systemctl start postgresql-15
复制
2 登录数据库及数据库的简单使用
检查postgresql数据库的操作系统用户
[root@localhost ~\]# cat /etc/passwd postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
复制
切换至数据库用户
[root@localhost ~\]# su - postgres -bash-4.2$
复制
登录数据库
-bash-4.2$ psql psql (15.1) Type "help" for help.
复制
显示现有数据库
List of databases Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges -----------+----------+----------+-------------+-------------+------------+-----------------+----------------------- postgres | postgres | UTF8 | en\_US.UTF-8 | en\_US.UTF-8 | | libc | template0 | postgres | UTF8 | en\_US.UTF-8 | en\_US.UTF-8 | | libc | =c/postgres + | | | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en\_US.UTF-8 | en\_US.UTF-8 | | libc | =c/postgres + | | | | | | | postgres=CTc/postgres (3 rows)
复制
退出到操作系统下,查看postgresql的后台进程
[root@localhost ~\]# top -b -u postgres -d 1 -n 1 -c top - 03:17:36 up 54 min, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 116 total, 2 running, 114 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.0 us, 0.0 sy, 0.0 ni,100.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 12028356 total, 10859232 free, 481980 used, 687144 buff/cache KiB Swap: 6160380 total, 6160380 free, 0 used. 11264344 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2070 postgres 20 0 401252 17368 15920 S 0.0 0.1 0:00.04 /usr/pgsql-15/bin/postmaster -D /var/lib/pgsql/15/d+ 2072 postgres 20 0 253084 2148 720 S 0.0 0.0 0:00.00 postgres: logger 2073 postgres 20 0 401404 2316 812 S 0.0 0.0 0:00.00 postgres: checkpointer 2074 postgres 20 0 401388 3368 1872 S 0.0 0.0 0:00.37 postgres: background writer 2076 postgres 20 0 401388 6272 4780 S 0.0 0.1 0:00.08 postgres: walwriter 2077 postgres 20 0 402872 3356 1588 S 0.0 0.0 0:00.00 postgres: autovacuum launcher 2078 postgres 20 0 402852 3088 1356 S 0.0 0.0 0:00.00 postgres: logical replication launcher
复制
[root@localhost ~\]# ps -fu postgres UID PID PPID C STIME TTY TIME CMD postgres 1010 1 0 01:02 ? 00:00:00 /usr/pgsql-15/bin/postmaster -D /var/lib/pgsql/15/data/ postgres 1067 1010 0 01:02 ? 00:00:00 postgres: logger postgres 1076 1010 0 01:02 ? 00:00:00 postgres: checkpointer postgres 1077 1010 0 01:02 ? 00:00:00 postgres: background writer postgres 1089 1010 0 01:02 ? 00:00:00 postgres: walwriter postgres 1090 1010 0 01:02 ? 00:00:00 postgres: autovacuum launcher postgres 1091 1010 0 01:02 ? 00:00:00 postgres: logical replication launcher
复制
看一下数据库状态
-bash-4.2$ export PATH=$PATH:/usr/pgsql-15/bin -bash-4.2$ pg\_ctl status pg\_ctl: server is running (PID: 1010) /usr/pgsql-15/bin/postgres "-D" "/var/lib/pgsql/15/data/"
复制
创建数据库,连接数据库,创建表
postgres=# create database test; CREATE DATABASE postgres=# \\c test You are now connected to database "test" as user "postgres". test=# create table test (id int, name varchar(20)); CREATE TABLE test=# insert into test values (1, 'test'); INSERT 0 1 test=# select \* from test; id | name ----+------ 1 | test (1 row)
复制
远程连接数据库,首先创建用户,授予权限
CREATE ROLE postgres=# alter database test owner to test; ALTER DATABASE test=# alter table test owner to test; ALTER TABLE
复制
这里直接把test数据库和test表的owner更换为新建的用户,由于数据库的owner是在创建表之后改的,表的owner也需要改一下。
编辑一下数据库data目录里的pg_hba.conf.conf,加入下面一行
host test all 192.168.0.0/16 md5
复制
第三列的all允许用户连接所有的数据库,这样test用户就可以从192.168网段的任一地址连接到数据库了
最后修改时间:2023-03-06 10:40:54
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。