Skip to content

Instantly share code, notes, and snippets.

@shellfly
Last active January 1, 2024 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shellfly/7bc588ad05df4876de7d to your computer and use it in GitHub Desktop.
Save shellfly/7bc588ad05df4876de7d to your computer and use it in GitHub Desktop.
Postgres initialize

Debian 7, Postgres 9.4

安装完后默认生成两个用户,一个系统用户postgres,一个数据库用户postgres,都没有密码,

系统用户postgres不要设置密码,修改数据库用户postgres密码

  1. $ sudo su postgres
  2. $ psql
  3. $ \password

创建数据库: CREATE DATABASE project_name;

创建用户: CREATE USER db_user_name WITH PASSWORD 'password';

设置权限: GRANT ALL PRIVILEGES ON DATABASE project_name TO db_user_name;

登录: psql -U db_user_name -d db_name

备份恢复

backup

-F c is custom format (compressed, and able to do in parallel with -j N) -b is including blobs, -v is verbose, -f is the backup file name

pg_dump -h localhost -p 5432 -U {user} -F c -b -v -f  `date +%Y-%m-%d`.backup {db}

docker exec souka-postgres-1 pg_dump -h localhost -p 5432 -U postgres souka  | gzip -9 > db-backup-$(date +%d-%m-%y).sql.gz

restore

pg_restore -h localhost -p 5432 -U postgres -d old_db -v "/usr/local/backup/10.70.0.61.backup"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment