数据库重新初始化步骤

数据库重新初始化步骤

重新初始化并恢复数据库 步骤:

  • 1、停服务
vmserver.sh stopall
  • 2、新建数据库目录并授权
cd /mnt/syncdata/pgsql
mkdir -p data_new
chown -R postgres.postgres data_new/
chmod 700 data_new
  • 3、检查
ll
total 8
drwx------ 19 postgres postgres 4096 Nov 28 17:19 data
drwx------ 2 postgres postgres 4096 Nov 28 19:04 data_new
  • 4、初始化数据库
su - postgres -c "/home/postgres/pgsql/bin/initdb -E UTF8 --locale=C -D /mnt/syncdata/pgsql/data_new" 
  • 5、复制配置文件
cp -pfb data/pg_hba.conf data_new/pg_hba.conf 
cp -bpf data/postgresql.conf data_new/postgresql.conf
  • 6、替换目录
[root@centos7-18 pgsql]# mv data data_origin
[root@centos7-18 pgsql]# mv data_new data
  • 7、修改配置文件的权限
[root@centos7-18 pgsql]# ll data/postgresql.conf data/pg_hba.conf
-rw------- 1 root root 101 Nov 28 06:03 data/pg_hba.conf
-rwx------ 1 root root 19600 Nov 28 06:03 data/postgresql.conf
[root@centos7-18 pgsql]# chown -R postgres.postgres data/postgresql.conf data/pg_hba.conf
  • 8、设置免密码登录修改
  cp /mnt/syncdata/pgsql/data/pg_hba.conf /mnt/syncdata/pgsql/data/pg_hba.conf_bak
sed -i 's/md5/trust/g' /mnt/syncdata/pgsql/data/pg_hba.conf
grep md5 /mnt/syncdata/pgsql/data/pg_hba.conf
grep trust /mnt/syncdata/pgsql/data/pg_hba.conf
local all all trust
host all all 127.0.0.1/32 trust
host all all ::1/128 trust
host all all 0.0.0.0/0 trust
pgsql_ctl restart
Stopping Postgresql: [ OK ]
Starting Postgresql: [ OK ]
  • 9、将postgres密码改为原来的密码
  alter user postgres password '配置文件中的密码';
unipd 1 `grep DBP /usr/local/svconfig/server/conf/imos.cfg|awk -F= '{print $2}'`
passwd
/home/postgres/pgsql/bin/psql -Upostgres
postgres=# alter user postgres password 'passwd'; --具体的值是上面查询出来的结果
ALTER ROLE
postgres=# \q
  • 10、恢复旧的数据库
  [root@centos7-18 pgsql]# imosdbbr.sh 
What do you want to do?
1.backup
2.recovery
3.vacuum
c.cancel
Please have a choice :2
Please enter DB address :127.0.0.1
recover fail! the reason is:
these databases:imos are not in the postgres in your system
please check or recover the database again
Are you determined to recover the database?(yes/no)
yes
Now,begin to recover database,please be patient and wait a moment...
  • 11、重启所有服务
  vmserver.sh startall