RockyLinux9.6二进制编译安装postgresql-17.5数据库-pgadmin4图形化管理工具
#安装依赖包
dnf install -y gcc gcc-c++ make libicu-devel bison flex readline-devel zlib-devel python3-devel openssl-devel libpq-devel
dnf groupinstall -y "Development Tools"
#编译报错及解决:
configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
#解决方法:
#dnf install -y readline-devel
编译报错:
configure: error: header file <Python.h> is required for Python
#解决方法:
dnf install -y python3-devel
#安装包获取地址: pgadmin4 postgresql-17.5 https://www.postgresql.org/download/ https://mirrors.tuna.tsinghua.edu.cn/postgresql/source/v15.7/postgresql-15.7.tar.gz https://ftp.postgresql.org/pub/source/v17.5/postgresql-17.5.tar.gz https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v9.5/source/pgadmin4-9.5.tar.gz
#root用户下操作: #创建用户、组 groupadd postgres useradd -g postgres postgres echo "123456"|passwd postgres --stdin cat /etc/passwd|grep postgres
#解压编译安装 tar -zxf postgresql-17.5.tar.gz -C /usr/local/src cd /usr/local/src/postgresql-17.5 #PostgreSQL9.X及之后的版本编译方式 ./configure --prefix=/usr/local/postgresql --with-perl --with-python && echo $? #使用所有CPU核心加速编译 make -j $(nproc) && make install && echo $? mkdir -p /usr/local/postgresql/data mkdir -p /usr/local/postgresql/logs chown -R postgres:postgres /usr/local/postgresql/
#postgres用户操作: su - postgres export PGHOME=/usr/local/postgresql export PGDATA=/usr/local/postgresql/data export PATH=$PGHOME/bin:$PATH #export PATH=/usr/local/postgresql/bin:$PATH #初始化postgresql数据库(推荐) #initdb -D $PGDATA --locale=en_US.UTF-8 --encoding=UTF8 #su - postgres -c "/usr/local/postgresql/bin/initdb -D $PGDATA --locale=en_US.UTF-8 --encoding=UTF8" #su - postgres -c "/usr/local/postgresql/bin/initdb -D /usr/local/postgresql/data --locale=en_US.UTF-8 --encoding=UTF8" $ initdb -D $PGDATA --locale=en_US.UTF-8 --encoding=UTF8 The files belonging to this database system will be owned by user "postgres". This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8". The default database encoding has accordingly been set to "UTF8". The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /usr/local/postgresql/data ... ok creating subdirectories ... ok selecting dynamic shared memory implementation ... posix selecting default "max_connections" ... 100 selecting default "shared_buffers" ... 128MB selecting default time zone ... Asia/Shanghai creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok
initdb: warning: enabling "trust" authentication for local connections initdb: hint: You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /usr/local/postgresql/data/ -l logfile start
#数据目录下文件内容(待补充) base/:数据库文件存储位置 pg_wal/:事务日志 pg_log/:错误日志
#手动启动停止
#启动postgresql
/usr/local/postgresql/bin/pg_ctl -D /usr/local/postgresql/data/ -l /usr/local/postgresql/logs/postgresql.log start
#停止postgresql
/usr/local/postgresql/bin/pg_ctl -D /usr/local/postgresql/data/ -l /usr/local/postgresql/logs/postgresql.log stop
#postgresql自带启停脚本
mkdir -p /etc/init.d/
cp -fr /usr/local/src/postgresql-17.5/contrib/start-scripts/linux /etc/init.d/postgresql
chmod +x /etc/init.d/postgresql
#修改postgresql脚本
#Installation prefix
prefix=/usr/local/postgresql
#Data directory
PGDATA="/usr/local/postgresql/data"
#Who to run postgres as, usually "postgres". (NOT "root")
PGUSER=postgres
#Where to keep a log file
PGLOG="/usr/local/postgresql/logs/postgresql.log"
#/etc/init.d/postgresql start
Starting PostgreSQL: ok
#/etc/init.d/postgresql status
pg_ctl: server is running (PID: 21908)
/usr/local/postgresql/bin/postgres "-D" "/usr/local/postgresql/data"
#/etc/init.d/postgresql stop
Stopping PostgreSQL: ok
#/etc/init.d/postgresql status
pg_ctl: no server running
#systemd管理服务 #cat /usr/lib/systemd/system/postgresql.service
[Unit]
Description=postgresql.service
After=network.target
[Service]
Type=forking
User=postgres
Group=postgres
WorkingDirectory=/usr/local/postgresql/
Environment=PGDATA=/usr/local/postgresql/data
Environment=PGLOG=/usr/local/postgresql/logs
ExecStart=/usr/local/postgresql/bin/pg_ctl start -D ${PGDATA} -l ${PGLOG}/postgresql.log
#ExecStart=/usr/local/postgresql/bin/pg_ctl start -D /usr/local/postgresql/data -l /usr/local/postgresql/logs/postgresql.log
ExecStop=/usr/local/postgresql/bin/pg_ctl stop -D ${PGDATA} -l ${PGLOG}/postgresql.log -m fast
#ExecStop=/usr/local/postgresql/bin/pg_ctl stop -D /usr/local/postgresql/data -l /usr/local/postgresql/logs/postgresql.log -m fast
ExecReload=/usr/local/postgresql/bin/pg_ctl reload -D ${PGDATA} -l ${PGLOG}/postgresql.log
#ExecReload=/usr/local/postgresql/bin/pg_ctl reload -D /usr/local/postgresql/data -l /usr/local/postgresql/logs/postgresql.log
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#启停命令
systemctl daemon-reload
systemctl enable postgresql.service --now
systemctl start postgresql.service
systemctl status postgresql.service
#systemctl list-unit-files --type=service --state=enabled|grep postgresql
postgresql.service enabled disabled
#systemctl is-enabled postgresql
enabled
#配置文件 /usr/local/postgresql/data/postgresql.conf listen_addresses = '*' port = 5432 /usr/local/postgresql/data/pg_hba.conf #IPv4 local connections: host all all 0.0.0.0/0 trust
$ pg_ctl -D /usr/local/postgresql/data/ -l /usr/local/postgresql/logs/postgresql.log start waiting for server to start.... done server started $ps -ef|grep postgresql postgres 82158 1 1 22:43 ? 00:00:00 /usr/local/postgresql/bin/postgres -D /usr/local/postgresql/data
#登陆数据库 [postgres@localhost logs]$ psql -U postgres -d test -h 127.0.0.1 -p 5432 psql (17.5) Type "help" for help. U:U是user的缩写,用于指定连接数据库的用户名,例如postgres是 PostgreSQL 默认的超级用户。 -d:d代表database,用来指定要连接的具体数据库名称。 -h:h表示host,用于指定数据库服务器的主机地址,localhost表示本地主机,若数据库部署在其他服务器,需填写对应 IP 地址或域名。 -p:p是port的缩写,指定数据库服务器的端口号,PostgreSQL 默认端口为5432 ,若修改过端口,则需填写实际端口。 -W: 交互方式输入密码
#列出用户/角色 \du SELECT usename FROM pg_user;
#查看用户表 postgres=# select * from pg_user; usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig ----------+----------+-------------+----------+---------+--------------+----------+----------+----------- postgres | 10 | t | t | t | t | ******** | | (1 row)
#修改postgres用户密码 postgres=# ALTER USER postgres WITH PASSWORD '123456'; ALTER ROLE postgres=# select * from pg_user; usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig ----------+----------+-------------+----------+---------+--------------+----------+----------+----------- postgres | 10 | t | t | t | t | ******** | | (1 row)
#查看数据库 \l -- 或完整命令 SELECT datname FROM pg_database;
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
(3 rows)
#创建数据库
postgres=# create database test;
CREATE DATABASE
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | Locale | ICU Rules | Access privileges
-----------+----------+----------+-----------------+-------------+-------------+--------+-----------+-----------------------
postgres | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
template0 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | | =c/postgres +
| | | | | | | | postgres=CTc/postgres
test | postgres | UTF8 | libc | en_US.UTF-8 | en_US.UTF-8 | | |
(4 rows)
#切换数据库 postgres-# \c test You are now connected to database "test" as user "postgres". test-#
#创建表格 CREATE TABLE USERS( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50) );
test=# CREATE TABLE USERS( test(# ID INT PRIMARY KEY NOT NULL, test(# NAME TEXT NOT NULL, test(# AGE INT NOT NULL, test(# ADDRESS CHAR(50) test(# ); CREATE TABLE
#查看创建的表 \dt -- 或完整命令 SELECT tablename FROM pg_tables WHERE schemaname = 'public';
test=# \d
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | USERS | table | postgres
(1 row)
test=# \dt
List of relations
Schema | Name | Type | Owner
--------+----------+-------+----------
public | USERS | table | postgres
(1 row)
#查看表详情 SELECT column_name, data_type, character_maximum_length, is_nullable, column_default FROM information_schema.columns WHERE table_name = 'users'; test=# \d USERS Table "public.USERS" Column | Type | Collation | Nullable | Default ---------+---------------+-----------+----------+--------- id | integer | | not null | name | text | | not null | age | integer | | not null | address | character(50) | | | Indexes: "USERS_pkey" PRIMARY KEY, btree (id)
#删除表 DROP TABLE USERS;
#插入数据 test=# INSERT INTO USERS (ID,NAME,AGE,ADDRESS) VALUES (1, 'Johnny', 18, 'China'); INSERT 0 1
#查看表数据
test=# SELECT * from USERS;
id | name | age | address
----+--------+-----+----------------------------------------------------
1 | Johnny | 18 | China
(1 row)
#查看表条数
test=# SELECT COUNT(*) from USERS;
count
-------
1
(1 row)
#修改表数据
UPDATE USERS SET AGE = 25 WHERE ID = 1;
test=# INSERT INTO USERS (ID,NAME,AGE,ADDRESS) VALUES (1, 'Johnny', 18, 'China');
INSERT 0 1
pgAdmin4安装
pgAdmin4是PostgreSQL的流行图形化管理工具。
参考:https://www.modb.pro/db/181174
rpm --import https://www.pgadmin.org/static/packages_pgadmin_org.pub rpm -i https://ftp.postgresql.org/pub/pgadmin/pgadmin4/yum/pgadmin4-redhat-repo-2-1.noarch.rpm dnf install -y pgadmin4-web #如果全部安装完成,执行下面的 pgAdmin4 设置脚本来创建一个新的管理员用户并为 pgAdmin4 设置 Apache/httpd 配置。 /usr/pgadmin4/bin/setup-web.sh #输入您的电子邮件地址和密码,为 pgAdmin4 创建一个新的管理员帐户。 #之后,键入“ y ”并按“ Enter ”以生成并启用 pgAdmin 的 Apache/httpd 配置。
[root@node1 ~]# /usr/pgadmin4/bin/setup-web.sh
Setting up pgAdmin 4 in web mode on a Redhat based platform...
Creating configuration database...
NOTE: Configuring authentication for SERVER mode.
Enter the email address and password to use for the initial pgAdmin user account:
Password:
Retype password:
pgAdmin 4 - Application Initialisation
======================================
Creating storage and log directories...
Configuring SELinux...
The Apache web server is not running. We can enable and start the web server for you to finish pgAdmin 4 installation. Continue (y/n)? y
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
Apache successfully enabled.
Apache successfully started.
You can now start using pgAdmin 4 in web mode at http://127.0.0.1/pgadmin4
#ps -ef|grep pgAdmin4
root 7765 1 0 18:54 ? 00:00:00 gpg-agent --homedir /var/cache/dnf/pgAdmin4-25072de53345df27/pubring --use-standard-socket --daemon
root 7767 7765 0 18:54 ? 00:00:00 scdaemon --multi-server --homedir /var/cache/dnf/pgAdmin4-25072de53345df27/pubring
#ps -ef|grep httpd
pgAdmin4 配置完成,它在 URL 路径“ http://server-ip/pgadmin4 ”中可用。
要验证 pgAdmin4 安装,您将访问 pgAdmin URL 路径安装并使用您的用户电子邮件和密码登录 pgAdmin。
打开您的网络浏览器,输入您的服务器 IP 地址,URL 路径为“/pgadmin4”,如下所示。
http://192.168.100.180/pgadmin4
#sql语句 #创建数据库 create database test;
#查看数据库 SELECT datname FROM pg_database;
#创建表 CREATE TABLE public.USERS( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50) );
#创建myschema实例(数据库>实例>表) create schema myschema;
#创建myschema实例 CREATE TABLE myschema.users( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50) );
#查看表 SELECT tablename FROM pg_tables WHERE schemaname = 'public'; SELECT tablename FROM pg_tables WHERE schemaname = 'myschema';
#查看表字段 SELECT column_name, data_type, character_maximum_length, is_nullable, column_default FROM information_schema.columns WHERE table_name = 'users';
#插入数据 INSERT INTO "public".users (ID,NAME,AGE,ADDRESS) VALUES (1, 'Johnny', 18, 'China'); INSERT INTO "myschema".users (ID,NAME,AGE,ADDRESS) VALUES (1, 'Johnny', 20, 'America');
#查询不同实例中的users表数据 select * from myschema.users; select * from public.users;
#修改表名 SELECT tablename FROM pg_tables WHERE schemaname = 'public'; ALTER TABLE IF EXISTS users RENAME TO users_new; ALTER TABLE users_new RENAME TO users;
#清空表数据 truncate table public.users; truncate table myschema.users;
#删除表 drop table if exists public.users;