pgsql
- 进入本地数据库
psql resumehack_development
- 进入服务器数据库
console 直接连数据库 bin/rails dbconsole production - 修改数据库密码
\password - 查看数据库大小
命令:
SELECT pg_size_pretty(pg_database_size('database name'));
查询所有表并按照大小排列
select table_name, pg_relation_size(quote_ident(table_name)) from information_schema.tables where table_schema = 'public' order by 2;
- 复制单张表数据
pg_dump -U postgres --table jd_temps -Fc test_production > /home/apps/jd_temps.sql
scp apps@wondermail.club:/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);
- 导入数据库到本地
psql resumehack_development < /Users/dailanyi/Desktop/PostgreSQL.sql
- restore db
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:
- 远程文件下载:
scp -r apps@staging.wondercv.com:/home/apps/main-backup/resumehack_pg/2018.12.02.15.30.02 /Users/dailanyi/Downloads/
- 上传本地文件到服务器
scp /path/local_filename username@servername:/path
- 在文件目录下跑 不在console里面
export RAILS_ENV=production
rails runner /home/apps/download_interview_data.rb