sql 基础(pgsql & mysql)

  1. 进入本地数据库

    1
    psql resumehack_development
  2. 进入服务器数据库
    console 直接连数据库 bin/rails dbconsole production

  3. 修改数据库密码
    password

  4. 查看数据库大小

    命令:

    1
    SELECT pg_size_pretty(pg_database_size('database name'));

查询所有表并按照大小排列

1
select table_name, pg_relation_size(quote_ident(table_name)) from information_schema.tables where table_schema = 'public' order by 2;
  1. 复制单张表数据
    pg_dump -U postgres –table jd_temps -Fc test_production > /home/apps/jd_temps.sql

scp [email protected]:/home/apps/jd_temps.sql /Users/dailanyi/Downloads/

psql resumehack_development
DROP TABLE jd_temps;

pg_restore -U dailanyi –dbname resumehack_development –table=jd_temps /Users/dailanyi/Downloads/jd_temps.sql

psql resumehack_development
ALTER TABLE jd_temps ADD PRIMARY KEY (id);

  1. 导入数据库到本地

    1
    psql resumehack_development < /Users/dailanyi/Desktop/PostgreSQL.sql
  2. restore db

    1
    2
    psql -U postgres -d wondercv_test -c "drop schema public cascade; create schema public;"
    psql --set ON_ERROR_STOP=on -U postgres wondercv_test < #{db_unzip_file}

mysql

从远程拷贝数据库到本地

1.先上远程查看是否有备份文档
没有就创建一个:
mysqldump -uroot -p密码 数据库名称 > 导出来的数据库文档名称.sql

2.在本地运行
scp -P 端口号 root@服务器名称:/opt/app/进程名称/current/导出来的数据库文档名称.sql .

3.将远程拷下来的数据库库备份文档倒入本地数据库
mysql -uroot -p本地数据库密码 本地数据库名称 < 导出来的数据库文档名称.sql

ps:

  1. 远程文档下载:

    1
    scp -r [email protected]:/home/apps/main-backup/resumehack_pg/2018.12.02.15.30.02 /Users/dailanyi/Downloads/
  2. 上传本地文档到服务器

    1
    scp /path/local_filename username@servername:/path
  3. 在文档目录下跑 不在console里面
    export RAILS_ENV=production
    rails runner /home/apps/download_interview_data.rb

原文链接 大专栏  https://www.dazhuanlan.com/2019/08/15/5d5514081e2ac/

猜你喜欢

转载自www.cnblogs.com/chinatrump/p/11416254.html