PostgreSQL:初始化数据库

news/2024/7/9 21:57:02 标签: 数据库, postgresql

一、初始化数据库的命令

#!/bin/bash

adduser postgres

PGHOME="/opt/common/postgresql" # PostgreSQL 命令的位置
datadir="/opt/data/pgdata-13.1" # 数据库文件的位置,在执行这个脚本前,这个目录必须不存在
mkdir -p ${datadir} # 创建目录结构

chown postgres:postgres ${datadir} -R

su - postgres -c "${PGHOME}/bin/initdb -D ${datadir} -E UTF8 --locale=C -U postgres" # 初始化数据库文件

su - postgres -c "${PGHOME}/bin/pg_ctl -D ${datadir} start" # 启动数据库

password=$(cat /proc/sys/kernel/random/uuid) # 生成一个随机密码
${PGHOME}/bin/psql -U postgres -h 127.0.0.1 -c "alter role postgres with password '${password}'" # 把数据库用户 postgres 的密码改为刚刚生成的随机密码

psql -U postgres -h 127.0.0.1 -p 5432 -c "create database dogdb with template = template0 ENCODING = UTF8" # 创建一个名为 dogdb 的数据库

echo "pg user postgres, password [${password}]" # 输出这个数据库密码

二、执行脚本,生成数据库文件

$ ./init-db.sh 
adduser: user 'postgres' already exists
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 "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /opt/data/pgdata-13.1 ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... UTC
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
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:

    /opt/common/postgresql/bin/pg_ctl -D /opt/data/pgdata-13.1 -l logfile start

waiting for server to start....2021-03-23 03:50:39.694 UTC [13712] LOG:  starting PostgreSQL 13.1 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2021-03-23 03:50:39.695 UTC [13712] LOG:  listening on IPv6 address "::1", port 5432
2021-03-23 03:50:39.695 UTC [13712] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2021-03-23 03:50:39.702 UTC [13712] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2021-03-23 03:50:39.710 UTC [13713] LOG:  database system was shut down at 2021-03-23 03:50:39 UTC
2021-03-23 03:50:39.716 UTC [13712] LOG:  database system is ready to accept connections
 done
server started
ALTER ROLE
CREATE DATABASE
pg user postgres, password [422fcc44-c85d-44ba-aa16-d899ac59e9b3]

从输出的信息可以看出,创建数据库成功,并且随机密码是:422fcc44-c85d-44ba-aa16-d899ac59e9b3

查看数据库创建结果:

$ /opt/common/postgresql/bin/psql -h 127.0.0.1 -U postgres -c "\l"
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 dogdb     | postgres | UTF8     | C       | C     | 
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(4 rows)

可以看到 dogdb 已经创建成功。


 


http://www.niftyadmin.cn/n/788663.html

相关文章

TP5+商城小程序——日志系统

QQ 1274510382 Wechat JNZ_aming 商业联盟 QQ群538250800 技术搞事 QQ群599020441 解决方案 QQ群152889761 加入我们 QQ群649347320 共享学习 QQ群674240731 纪年科技aming 网络安全 ,深度学习,嵌入式,机器强化,生物智能,生命科学。

Python:使用 csv 模块读写 csv 文件

目录 一、读取 csv 文件 二、写入 csv 文件 一、读取 csv 文件 # -*- coding: utf-8 -*-import csv import sysdef read_csv_file(filename):with open(filename, r) as f:csv_reader csv.reader(f)header_row next(csv_reader)print(header_row) # 输出文件头cnt 0for l…

态势感知——活跃IP段探测脚本集合【多语言】

QQ 1274510382 Wechat JNZ_aming 商业联盟 QQ群538250800 技术搞事 QQ群599020441 解决方案 QQ群152889761 加入我们 QQ群649347320 共享学习 QQ群674240731 纪年科技aming 网络安全 ,深度学习,嵌入式,机器强化,生物智能,生命科学。 echo off setlocal cls color 4f&MODE c…

TP5+商城小程序——全局异常处理的应用AOP思想

QQ 1274510382 Wechat JNZ_aming 商业联盟 QQ群538250800 技术搞事 QQ群599020441 解决方案 QQ群152889761 加入我们 QQ群649347320 共享学习 QQ群674240731 纪年科技aming 网络安全 ,深度学习,嵌入式,机器强化,生物智能,生命科学。 ###AOP 面向切面编程

Hbase+Solr实现二级索引提供高效查询

[sizemedium] 接着上一篇介绍协处理器的文章[url]http://qindongliang.iteye.com/blog/2277145[/url],本篇我们来实战一个例子,看下如何使用协处理来给Hbase建立二级索引。github地址:[url]https://github.com/qindongliang/hbase-increment-…

nodejs Stream 的操作

从流中读取数据 创建 main.js 文件, 代码如下: var fs require("fs"); var data ; // 创建可读流 var readerStream fs.createReadStream(input.txt); // 设置编码为 utf8。 readerStream.setEncoding(UTF8); // 处理流事件 --> data, end, and err…

Linux操作系统——vmware安装CentOS

5A02H-AU243-TZJ49-GTC7K-3C61N 选择i wil configure partitioning(我将会配置分区),然后点击done 桥接模式网络配置 1、配置ip地址等信息在/etc/sysconfig/network-scripts/ifcfg-ens33文件里做如下配置: 命令: vi…

渗透工具-OWASP ZAP

OWASP Zed攻击代理(ZAP)是世界上最受欢迎的免费安全审计工具之一, 由数百名国际志愿者*积极维护。 它可以帮助您在开发和测试应用程序时自动查找Web应用程序中的安全漏洞。 也可以说:ZAP是一个中间人代理。 它允许您查看您对Web应…