Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where did my database go in postgres?

postgres refuses to work. I am using 9.2 and a newbie.

I create a database. I list and its not there? There is no error! Where did it go? Was it ever created?

postgres-# creatdb test
postgres-# \list
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_PH.UTF-8 | en_PH.UTF-8 | 
 template0 | postgres | UTF8     | en_PH.UTF-8 | en_PH.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_PH.UTF-8 | en_PH.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

postgres-# 



postgres@ubuntu:/home/ubuntu$ psql -h 127.0.0.1 -U postgres -d test
Password for user postgres: 
psql: FATAL:  database "test" does not exist
like image 390
Tampa Avatar asked Sep 05 '13 02:09

Tampa


People also ask

How do I show all databases in PostgreSQL?

Step 1: Log in to the server using the SQL Shell (psql) app. Step 2: Run the following query: SELECT datname FROM pg_database; psql runs the query against the server and displays a list of existing databases in the output.

Does PostgreSQL store data?

I was recently asked where PostgreSQL actually stores data on disk. So it's time to share this... At the basic level, your data is simply stored as a set of files on disk. If you want a backup you can simply stop the database, copy all the database data files that are on your disk and you create a backup.

How do I restore a PostgreSQL database from SQL?

To restore a PostgreSQL database, you can use the psql or pg_restore utilities. psql is used to restore text files created by pg_dump whereas pg_restore is used to restore a PostgreSQL database from an archive created by pg_dump in one of the non-plain-text formats (custom, tar, or directory).


2 Answers

You have two errors:

  1. createdb is an operating system command, it's not a SQL command. In psql you need to use SQL statements and that would be: CREATE DATABASE. For details see the manual: http://www.postgresql.org/docs/current/static/sql-createdatabase.html

  2. Each SQL statement needs to be terminated with a ;. As you didn't do that, your statement wasn't executed and thus you didn't get an error. For details see the manual: http://www.postgresql.org/docs/current/static/sql-syntax-lexical.html

postgres=# createdb test;
ERROR:  syntax error at or near "createdb"
LINE 1: createdb test;
        ^
postgres=# create database test;
CREATE DATABASE
postgres=# \list
                                          List of databases
   Name    |  Owner   | Encoding |       Collate       |        Ctype        |   Access privileges
-----------+----------+----------+---------------------+---------------------+-----------------------
 postgres  | postgres | UTF8     | German_Germany.1252 | German_Germany.1252 | 
 template0 | postgres | UTF8     | German_Germany.1252 | German_Germany.1252 | =c/postgres          +
           |          |          |                     |                     | postgres=CTc/postgres
 template1 | postgres | UTF8     | German_Germany.1252 | German_Germany.1252 | postgres=CTc/postgres+
           |          |          |                     |                     | =c/postgres
 test      | postgres | UTF8     | German_Germany.1252 | German_Germany.1252 |
 10 rows)


postgres=#
like image 194
a_horse_with_no_name Avatar answered Oct 05 '22 01:10

a_horse_with_no_name


createdb is a shell command not the postgres command that's why it does not show any error or success message.

To create database from your terminal you have to do this.

sudo -u postgres createdb databaseName

To create database from postgres command you have to run the following command one after another

sudo -u postgres psql
CREATE DATABASE testDB;

If database create successfully then you will get a success message by "CREATE DATABASE"

enter image description here

To see the list of your database write this command

\l
like image 31
Ahmad Sharif Avatar answered Oct 05 '22 01:10

Ahmad Sharif