0

I'm trying to create a database in PostgreSQL using this command:

createdb -E UTF8 aws-inventories

There are no errors from that command. But when I go to list the dbs the new database isn't there:

postgres-# \l
                                                     List of databases
       Name        |  Owner   | Encoding |          Collate           |           Ctype            |   Access privileges
-------------------+----------+----------+----------------------------+----------------------------+-----------------------
 analysis          | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 animals           | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 postgis_31_sample | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 postgres          | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 template0         | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
                   |          |          |                            |                            | postgres=CTc/postgres
 template1         | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
                   |          |          |                            |                            | postgres=CTc/postgres
(6 rows)

And if I try to change to that new database it tells me that it isn't there:

postgres-# \c aws-inventories
FATAL:  database "aws-inventories" does not exist
Previous connection kept

I also tried this with the CREATE DATABASE command. And I get the same result:

postgres=# CREATE DATABASE aws-inventories
postgres-# \l
                                                     List of databases
       Name        |  Owner   | Encoding |          Collate           |           Ctype            |   Access privileges
-------------------+----------+----------+----------------------------+----------------------------+-----------------------
 analysis          | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 animals           | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 postgis_31_sample | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 postgres          | postgres | UTF8     | English_United States.1252 | English_United States.1252 |
 template0         | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
                   |          |          |                            |                            | postgres=CTc/postgres
 template1         | postgres | UTF8     | English_United States.1252 | English_United States.1252 | =c/postgres          +
                   |          |          |                            |                            | postgres=CTc/postgres
(6 rows)


postgres-# \c aws-inventories
FATAL:  database "aws-inventories" does not exist
Previous connection kept

Doing the same commands in PGAdmin4 I do get an error:

createdb aws-inventories
ERROR:  syntax error at or near "createdb"
LINE 1: createdb aws-inventories
        ^
SQL state: 42601
Character: 1**strong text**

CREATE DATABASE aws-inventories
ERROR:  syntax error at or near "-"
LINE 1: CREATE DATABASE aws-inventories
                           ^
SQL state: 42601
Character: 20

I'm on PostgreSQL 13. Why is Postgres refusing to create this database?

bluethundr
  • 1,005
  • 17
  • 68
  • 141
  • 1
    Did createdb not report any errors? – jjanes Apr 25 '21 at 02:06
  • Nope! No errors on the CLI. But if I try this in PGAdmin4 I get the following:`ERROR: syntax error at or near "createdb" LINE 1: createdb aws-inventories ^ SQL state: 42601 Character: 1` – bluethundr Apr 25 '21 at 02:12
  • Yet on the CLI nothing happens when I give the command: `postgres-# createdb -E UTF8 aws-inventories postgres-#`. No errors! It's just that the DB does not exist when I try to list it or change to it! – bluethundr Apr 25 '21 at 02:13
  • 1
    createdb needs to run from the OS shell prompt, not from the psql prompt. It didn't report any errors because you didn't end the command with a `;`, so it never actually executed anything. It is still waiting for you to finish typing the rest of your command. You **can** create databases from the psql prompt, but you have to do it with `CREATE DATABASE....;`, not `createdb`. – jjanes Apr 25 '21 at 02:16
  • Ok thanks. I works from the `psql` command prompt if I use the full `CREATE_DATABASE` command with a `;`. However it didn't like the hyphen `-` and I had to name it with an underscore `_`: This didn't work: `postgres=# CREATE DATABASE aws-inventories; ERROR: syntax error at or near "-" LINE 1: CREATE DATABASE aws-inventories;` But this DID work: `postgres=# CREATE DATABASE aws_inventories; CREATE DATABASE`. Thanks for the help! Put that as the answer and I'll accept it. :) – bluethundr Apr 25 '21 at 02:22
  • Are you sure that `createdb` is talking to the same server as your `psql` command? – Schwern Apr 25 '21 at 02:38

0 Answers0