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
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.
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.
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).
You have two errors:
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
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=#
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"
To see the list of your database write this command
\l
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With