Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgresql not creating db with “createdb” as superuser, yet not outputting errors [duplicate]

People also ask

How do I give superuser permissions in PostgreSQL?

Log into PostgreSQL and run the following ALTER USER command to change user test_user to superuser. Replace test_user with username as per your requirement. postgres-# ALTER USER test_user WITH SUPERUSER; In the above command, we use WITH SUPERUSER clause to change user to superuser.

What is Createdb in PostgreSQL?

createdb creates a new PostgreSQL database. Normally, the database user who executes this command becomes the owner of the new database. However, a different owner can be specified via the -O option, if the executing user has appropriate privileges. createdb is a wrapper around the SQL command CREATE DATABASE .


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 ;


Late to the party, but the accepted answer doesn't explain why no error is displayed. And as this is something Postgres newcomers often stumble upon, I wanted to add that.


TL/TR: always end your SQL statements with ;


Because the createdb database did not end with ; psql thinks the statement isn't finished and waits for more input. This is indicated by the prompt changing from postgres=# to postgres-#. An extremely subtle change that I wish psql would do differently (more "prominent").

By entering the meta-command \list the "current" SQL statement is "aborted" without executing it.

If the createdb had been ended with a ; the output would have been:

postgres=> createdb foobar;
ERROR:  syntax error at or near "createdb"
LINE 1: createdb foobar;
        ^
postgres=>

Clearly showing that something was wrong.


Using a node terminal, I had to run:

psql -U postgres 

[enter your password]

then ...

CREATE DATABASE dbadmin;

What is confusing is that I entered these same commands before and it didn't work. Only after logging out and logging back in, was I able to use this standard command from the documentation: https://www.postgresql.org/docs/10/tutorial-createdb.html


I was in this situation not long ago. In case someone else experiences this, considering that the command prompt shows postgres-# you can execute the pending createdb command by simply typing ; and the return key.