Postgres - drop/add 数据库的 npm 脚本
Postgres - npm script to drop/add database
我想简化我的生活并通过 npm 脚本自动执行 adding/dropping 我的测试数据库的过程,但是我 运行 遇到了问题。
尝试 1:
"drop-db:local": "psql postgres \"drop database blog_db; create database blog_db; \c blog_db; CREATE EXTENSION \"pgcrypto\";\""
在 运行 之后,我不断收到以下错误
psql: error: could not connect to server: FATAL: Peer authentication failed for user "drop database blog_db; create database blog_db; \c "
尝试 2:
改变了
psql postgres
至
psql -h localhost -U rm postgres
所以这会在我的终端中打开数据库,但这似乎忽略了下面消息中提到的一些内容
psql: warning: extra command-line argument "drop database blog_db; create database blog_db; \c blog_db; CREATE EXTENSION pgcrypto;" ignored
我做错了什么?
这是我的数据库用户列表
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
rm | Superuser, Create DB | {}
数据库版本:psql (12.2 (Ubuntu 12.2-2.pgdg18.04+1))
您需要在 psql 命令中使用 -c 或 -f。
如 psql 帮助所示:
-c, --command=COMMAND run only single command (SQL or internal) and exit
-f, --file=FILENAME execute commands from file, then exit
由于您正在使用多个命令,因此最好使用 -f 后跟包含所有命令的 sql 文件名,例如您的 drop_create_db.sql 文件可以包含以下代码:
drop database blog_db;
create database blog_db;
\c blog_db;
CREATE EXTENSION "pgcrypto";
您可以使用以下命令运行此文件
"drop-db:local": psql -U postgres -d postgres -p 5432 -f /tmp/drop_create_db.sql
披露:我为 EnterpriseDB (EDB)
工作
我想简化我的生活并通过 npm 脚本自动执行 adding/dropping 我的测试数据库的过程,但是我 运行 遇到了问题。
尝试 1:
"drop-db:local": "psql postgres \"drop database blog_db; create database blog_db; \c blog_db; CREATE EXTENSION \"pgcrypto\";\""
在 运行 之后,我不断收到以下错误
psql: error: could not connect to server: FATAL: Peer authentication failed for user "drop database blog_db; create database blog_db; \c "
尝试 2:
改变了
psql postgres
至
psql -h localhost -U rm postgres
所以这会在我的终端中打开数据库,但这似乎忽略了下面消息中提到的一些内容
psql: warning: extra command-line argument "drop database blog_db; create database blog_db; \c blog_db; CREATE EXTENSION pgcrypto;" ignored
我做错了什么?
这是我的数据库用户列表
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
rm | Superuser, Create DB | {}
数据库版本:psql (12.2 (Ubuntu 12.2-2.pgdg18.04+1))
您需要在 psql 命令中使用 -c 或 -f。 如 psql 帮助所示:
-c, --command=COMMAND run only single command (SQL or internal) and exit
-f, --file=FILENAME execute commands from file, then exit
由于您正在使用多个命令,因此最好使用 -f 后跟包含所有命令的 sql 文件名,例如您的 drop_create_db.sql 文件可以包含以下代码:
drop database blog_db;
create database blog_db;
\c blog_db;
CREATE EXTENSION "pgcrypto";
您可以使用以下命令运行此文件
"drop-db:local": psql -U postgres -d postgres -p 5432 -f /tmp/drop_create_db.sql
披露:我为 EnterpriseDB (EDB)
工作