简体   繁体   English

Postgres - 在 github 操作中运行 \c 命令

[英]Postgres - run \c command in github actions

In GitHub actions, I am running a JavaScript file which connects to PostgreSQL and creates the table and extension for the database.在 GitHub 操作中,我正在运行一个 JavaScript 文件,该文件连接到 PostgreSQL 并为数据库创建表和扩展名。

my script looks like this:我的脚本如下所示:

const { Client } = require('pg')

const pgclient = new Client({
  host: process.env.POSTGRES_HOST,
  port: process.env.POSTGRES_PORT,
  user: process.env.POSTGRES_USER,
  password: process.env.POSTGRES_PASSWORD,
  database: process.env.POSTGRES_DB,
})

pgclient.connect()

const createDB = `
    drop database mydb;
    create database mydb;
    \c mydb;
    CREATE EXTENSION "pgcrypto";
`

pgclient.query(createDB, (err, res) => {
  if (err) throw err
  pgclient.end()
})

When I run the script, I get an error当我运行脚本时,我得到一个错误

error: syntax error at or near "c"

Which I am guessing is coming from \c flag.我猜这是来自\c标志。

How do I use PostgreSQL commands like this?如何使用这样的 PostgreSQL 命令?

you can not use \c here because it is a psql meta-command, which I think you do not use here: See https://www.postgresql.org/docs/current/app-psql.html .你不能在这里使用 \c 因为它是一个 psql 元命令,我认为你在这里不使用它:参见https://www.postgresql.org/docs/current/app-psql.ZFC35FDC70D5FC69D263EZ588A

You need to reconnect to the new DB like so:您需要像这样重新连接到新数据库:

const pgclient_mydb = new Client({
  host: process.env.POSTGRES_HOST,
  port: process.env.POSTGRES_PORT,
  user: process.env.POSTGRES_USER,
  password: process.env.POSTGRES_PASSWORD,
  database: 'mydb',
})

pgclient_mydb.connect()

See also https://stackoverflow.com/a/43670984/10743176另请参阅https://stackoverflow.com/a/43670984/10743176

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM