加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSql 常用命令

发布时间:2020-12-13 18:15:33 所属栏目:百科 来源:网络整理
导读:Installation https://github.com/railscasts/337-capistrano-recipes/blob/master/blog-after/config/recipes/postgresql.rbhttp://whatcodecraves.com/articles/2008/02/05/setup-rails-with-postgresql sudo add-apt-repository ppa:pitti/postgresql -ys

Installation

https://github.com/railscasts/337-capistrano-recipes/blob/master/blog-after/config/recipes/postgresql.rbhttp://whatcodecraves.com/articles/2008/02/05/setup-rails-with-postgresql

sudo add-apt-repository ppa:pitti/postgresql -y
sudo apt-get -y update
sudo apt-get -y install postgresql libpq-dev

sudo -u postgres psql -c "create user xxx with password xxx;"
sudo -u postgres psql -c "create database xxx owner xxx;"

drop database:

psql -U luna -d xxx -W -c "drop database xxx"


[编辑]Postgres数据库常用操作命令

psql连接数据库:psql -d 'database name' -U 'user name' -W

删除数据库:dropdb'database name'

或者:sudo -u postgres psql -c "drop database'database name'"

创建数据库(luna用户作为拥有者):createdb -O'user name' 'database name'

查看数据库中的所有表: psql登录进去后,执行d

执行表users信息:d users

修改数据库用户的密码:ALTER USER luna with password 'secure-password';


[编辑]Backup & Restore

official backup:http://www.postgresql.org/docs/9.1/static/backup-dump.html#BACKUP-DUMP-ALL

File System Level Backup can only be done while database is down,in order to ensure the integrity and up-to-date for backups. READ:http://www.postgresql.org/docs/9.1/static/backup-file.html

example crontab backup:

26 * * * * sudo -u postgres pg_dumpall --clean > /tmp/db_dump_all.pgdump

example restore:

sudo -u postgres pgsql -f /tmp/db_dump_all.pgdump



osx install,create database and user

http://blog.willj.net/2011/05/31/setting-up-postgresql-for-ruby-on-rails-development-on-os-x/

更有封装好的shell command

sudo -u postgres +

createuser -P laoban #创建用户带密码 Enter 后输入密码

dropuser 'xxx' #删除用户

删除数据库

 dropdb demo
创建数据库 并分配给指定用户

createdb -O username database_name

导入数据库

psql dbname < infile

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读