postgresql 从命令行创建数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30641512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:59:03  来源:igfitidea点击:

Create database from command line

postgresql

提问by Juan Reina Pascual

I′m trying to create a data base from command line. My OS is centos and postgres version is 10.9.

我正在尝试从命令行创建一个数据库。我的操作系统是 centos,postgres 版本是 10.9。

sudo -u postgres psql createdb test
Password for user test:

Why asking me by user?

为什么用户问我?

回答by django-renjith

Change the user to postgres :

将用户更改为 postgres :

su - postgres

Create User for Postgres

为 Postgres 创建用户

$ createuser testuser

Create Database

创建数据库

$ createdb testdb

Acces the postgres Shell

访问 postgres Shell

psql ( enter the password for postgressql)

Provide the privileges to the postgres user

为 postgres 用户提供权限

$ alter user testuser with encrypted password 'qwerty';
$ grant all privileges on database testdb to testuser;

回答by tulsluper

Try:

尝试:

sudo -u postgres psql -c 'create database test;'

回答by Arif Usman

createdbis a command line utility which you can run from bash and not from psql. To create a database from psql, use the create database statement like so:

createdb是一个命令行实用程序,您可以从 bash 而不是从 psql 运行它。要从 psql 创建数据库,请使用 create database 语句,如下所示:

create database [databasename];

Note: be sure to always end your SQL statements with ;

注意:请务必始终以 ;

回答by Antony

As some of the answers point out, createdbis a command line utility that could be used to create database.

正如一些答案指出的那样,createdb是一个可用于创建数据库的命令行实用程序。

Assuming you have a user named dbuser, the following command could be used to create a database and provide access to dbuser:

假设您有一个名为 的用户dbuser,以下命令可用于创建数据库并提供对 的访问dbuser

createdb -h localhost -p 5432 -U dbuser testdb

Replace localhostwith your correct DB host name, 5432with correct DB port, and testdbwith the database name you want to create.

替换localhost为正确的数据库主机名、5432正确的数据库端口以及testdb要创建的数据库名称。

Now psqlcould be used to connect to this newly created database:

现在psql可以用于连接到这个新创建的数据库:

psql -h localhost -p 5432 -U dbuser -d testdb

Tested with createdband psqlversions 9.4.15.

createdbpsql版本测试9.4.15

回答by Gaurav Sachdeva

As the default configuration of Postgres, a user called postgresis made and the user postgreshas full super admin access to entire PostgreSQL instance running on your OS.

作为 Postgres 的默认配置,创建了一个名为postgres的用户,用户postgres对在您的操作系统上运行的整个 PostgreSQL 实例具有完全的超级管理员访问权限。

sudo -u postgres psql

The above command gets you the psql command line interface in admin mode.

上面的命令让你进入管理模式的 psql 命令行界面。

Creating user

创建用户

sudo -u postgres createuser <username>

Creating Database

创建数据库

 sudo -u postgres createdb <dbname>

NOTE: < > are not to be used while writing command, they are used just to signify the variables

注意:< > 不能在写命令时使用,它们只是用来表示变量

回答by James Wierzba

PGPORT=5432
PGHOST="my.database.domain.com"
PGUSER="postgres"
PGDB="mydb"
createdb -h $PGHOST -p $PGPORT -U $PGUSER $PGDB