简体   繁体   English

如何在远程linux机器上运行.sql脚本以在postgresql中创建数据库?

[英]How to run .sql script in remote linux machine to create database in postgresql?

I have a .sql script for postgresql and want to run it on linux remote machine. 我有一个用于postgresql的.sql脚本,想在Linux远程计算机上运行它。 I am using postgres 9.4. 我正在使用postgres 9.4。

In this script i have included database name and the table detail also. 在此脚本中,我还包含了数据库名称和表详细信息。

I just want to run this script so that it will create database and tables at the same time. 我只想运行此脚本,以便它可以同时创建数据库和表。

So, please tell me the command to run this script file on remote machine. 因此,请告诉我在远程计算机上运行此脚本文件的命令。

I tried following:- 我尝试了以下内容:

psql -d myDataBase -a -f myInsertFile

In my case

psql -a -f db_script_ddl.sql

But it is not working. 但这是行不通的。

It is asking for the data base database name. 它要求数据库数据库名称。 But i do not want to pass any database name. 但是我不想传递任何数据库名称。 I want script to create and connect to database and then to create tables. 我希望脚本创建并连接到数据库,然后创建表。

Following is what i have inside the script file to create database and table:- 以下是我在脚本文件中用于创建数据库和表的内容:-

DROP DATABASE IF EXISTS testDB;

CREATE DATABASE testDB;
\c testDB
Other commands to create tables.

But it is not working. 但这是行不通的。

Help me out. 帮帮我。

Please put one example, it will be easy to understand. 请举一个例子,这将很容易理解。

Thanks. 谢谢。

psql must connect to a database. psql 必须连接到数据库。 There is no option to connect to "no database in particular". 没有选项可以连接到“特别是没有数据库”。

Use the postgres database if you want to drop/create a database and switch to it. 如果要删除/创建数据库并切换到该数据库,请使用postgres数据库。

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

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