如何通过节点和pg导入PostgreSQL数据库转储

时间:2017-06-19 21:18:08

标签: sql node.js postgresql

我正在尝试使用节点中的pg包将PostgreSQL数据库转储(保存为纯文本文件)导入psql。

到目前为止,我在文件中读取字符串,然后尝试通过以下方法导入字符串:

public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
String provider;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    provider = locationManager.getBestProvider(new Criteria(), true);


    Location location =locationManager.getLastKnownLocation(provider);
    if (location!=null){
        Log.i("Location","Found");
    }
    else{
        Log.i("Location","Not Found");
    }


}

我收到以下错误:var sql = fs.readFileSync('./dbDumpOutput').toString(); pg.connect('postgres://localhost:5432/testdb', function(err, client, done){ if(err){ console.log('error: ', err); process.exit(1); } client.query(sql, function(err, result){ done(); if(err){ console.log('error: ', err); process.exit(1); } process.exit(0); });

这是我的dbdump的格式问题,我必须解析,或者我做错了什么?

2 个答案:

答案 0 :(得分:1)

这是因为纯文本pg_dump使用COPY FROM STDIN,最终以\.结尾。我认为你不能使用这个COPY FROM STDIN brianc的pg模块(否则this不需要)。您可以尝试在pg_dump上指定--inserts来生成插入。但我建议只使用pg_dump生成转储的工具来恢复,如psql

答案 1 :(得分:0)

我得到了一个工作解决方案,我在下面提供了,使用psql而不是pg。

const { spawn } = require('child_process');

const child = spawn('createdb', ['psqltest']);

child.on('exit', function (code, signal) {
  console.log('child process exited with ' +
              `code ${code} and signal ${signal}`);
  const cat = spawn('cat',['dbDumpOutput']);
  const imp = spawn('psql',['psqltest']);
  cat.stdout.pipe(imp.stdin);
});