286

I am trying to create a database from command line. My OS is centos and postgres version is 10.9.

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

Why is it prompting me for the password?

1

11 Answers 11

384

Change the user to postgres :

su - postgres

Create User for Postgres (in the shell and NOT with psql)

$ createuser testuser

Create Database (same)

$ createdb testdb

Acces the postgres Shell

psql ( enter the password for postgressql)

Provide the privileges to the postgres user

$ alter user testuser with encrypted password 'qwerty';
$ grant all privileges on database testdb to testuser;
Sign up to request clarification or add additional context in comments.

4 Comments

Important: createuser and createdb are run in the shell and NOT with psql.
But if run with psql, the correct commands are: create user testuser; create database testdb;
If the name of user is 'user', Can it be run as "$ alter user user with encrypted password 'qwerty';"
at least in Window you have to pay attention to lower/upper case. If you have capital letters in user's name, you have to use quotes
161

Try:

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

3 Comments

do not forget to grant permissions as well: sudo -u postgres psql -c 'grant all privileges on database test to username;'
On mac, it's just psql -c 'create database test;'
@JoshuaSwain That could be done if you have a postgresql user with the same name with your shell user with the privilege to create new database.
94

createdb is 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:

create database [databasename];

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

2 Comments

Plus one for the hint about ending with ;
Just wanted to comment that Postgres makes UPPERCASE to lower. So CREATE DATABASE FOO would actually create database named foo. It took me like 5 minutes to figure out why "database doesn't exists". see: stackoverflow.com/questions/43111996/…
60

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

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

createdb -h localhost -p 5432 -U dbuser testdb

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

Now psql could be used to connect to this newly created database:

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

Tested with createdb and psql versions 9.4.15.

2 Comments

This was the right way to create a database on an instance I can't ssh to. All the attempts to run "CREATE DB" as -c param for remote psql utility assumed the existence of a database named like the user
This was the way that worked for me when using docker and wanting to add a 2nd database to the one that was added as part of the init of the postgres image: docker exec -it postgres-container sh -c "createdb -h YOUR_HOST -p YOUR_PORT -U YOUR_USER YOUR_DATABASE"
24

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

sudo -u postgres psql

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

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

Comments

12

PostgreSQL Create Database - Steps to create database in Postgres.

  1. Login to server using postgres user.
    su - postgres
  2. Connect to postgresql database.
bash-4.1$ psql
psql (12.1)
Type "help" for help.
postgres=#
  1. Execute below command to create database.
CREATE DATABASE database_name;

Check for detailed information below: https://orahow.com/postgresql-create-database/

1 Comment

Please notice the ; character in the end of the line! It is important that you type it! Verify that the db was indeed created by using \l
6

For example, first, you can create the user(role) john with the password banana as shown below. *By default, PostgreSQL needs a password so you should set PASSWORD and you can omit WITH which is optional and you must use '' for PASSWORD instead of "" otherwise there is error:

CREATE ROLE john WITH LOGIN PASSWORD 'banana';

Then, you can create apple database with the owner john as shown below. *If you omit OWNER, the current user(role) is the database owner:

CREATE DATABASE apple OWNER john;

In addition, if you want to drop the user(role) john, you need to remove the owner john from apple database by replacing it with other (e.g., david), then drop the user(role) john as shown below otherwise there is error. *My answer explains how to drop a user(role) more properly:

ALTER DATABASE apple OWNER TO david;
DROP ROLE john;

Or:

REASSIGN OWNED BY john TO david;  
DROP ROLE john;

In addition again, you can drop the database as shown below:

DROP DATABASE apple;

Comments

5

With a single command line:

su -c "createuser dbuser;createdb -h localhost -p 5432 -E UTF8 -O dbuser dbname;" - postgres

Comments

4

It is pretty simple but sometimes I find the answers tricky.
[For windows users]
Open Windows cmd

  • psql -U <username>

  • Once connected to psql, enter the following command to create a new database:
    CREATE DATABASE <database_name>;

  • To verify that the database has been created, you can run the \l command to list all available databases. Your new database should be listed in the output.
    ------[Additional]------

  • You can now connect to the new database using the \c command followed by the database name, like this:
    \c <database_name>

  • You can now run SQL commands on the new database to create tables, insert data, and so on.
    Note: Make sure to replace <username>, <database_name> with your actual Postgres username and database name.

1 Comment

notice not to forget the ; at the end.
3
PGPORT=5432
PGHOST="my.database.domain.com"
PGUSER="postgres"
PGDB="mydb"
createdb -h $PGHOST -p $PGPORT -U $PGUSER $PGDB

1 Comment

If you change 'PGDB' to 'PGDATABASE' and export them (or prefix them to the command), then you don't need to pass these to the command, it'll read them from the environment.
1

If you are using pgAdmin:

In query editor you can try like this :

CREATE DATABASE <databasename>
    WITH 
    OWNER = <dbowner>
    ENCODING = <encoding>
    CONNECTION LIMIT = <numberofsimulaneousconnections>;

an example snippet :

CREATE DATABASE twitterdb
    WITH 
    OWNER = postgres
    ENCODING = 'UTF8'
    CONNECTION LIMIT = -1;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.