Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the psql command to list, create, use and examine databases?

Tags:

I'm a postgreSQL newbie and I can't find any usable introduction to using the psql command. At least I think that's the command I want to use.

Is it possible in postgreSQL to simply connect to the server and then list, create, use and examine databases?

I'd like to be able to use psql to do something like this with MySQL (I've deleted many extra lines):

Connect without specifying a database - I can't seem to do that with psql:

$ mysql -u root -prootpassword Welcome to the MySQL monitor.  Commands end with ; or \g. Server version: 5.5.28 MySQL Community Server (GPL) 

I can list databases with mysql but the posgreSQL command SHOW doesn't seem to do it.

mysql> show databases; +--------------------+ | Database           | +--------------------+ | information_schema | | mysql              | | ocdp               | | performance_schema | | test               | +--------------------+ 5 rows in set (0.04 sec) 

I can switch databases.

mysql> use ocdp; Database changed 

I can't figure out this command in psql:

mysql> show tables; +---------------------------------+ | Tables_in_ocdp                  | +---------------------------------+ | OCAddresses                     | | OCStreets                       | +---------------------------------+ 2 rows in set (0.00 sec) 

I think I can do this in psql with 'createdb' and 'dropdb' commands:

mysql> create database foo; Query OK, 1 row affected (0.00 sec)  mysql> drop database foo; Query OK, 0 rows affected (0.03 sec) 

I use \quit

mysql> quit Bye 

The answer to these questions should take only a moment for someone who knows postgreSQL but I just can't find documentation anywhere that shows how to do these simple operations. Maybe I shouldn't even be using psql at all for this?

like image 761
zabouti Avatar asked Feb 05 '13 19:02

zabouti


People also ask

How do I list all databases and tables using psql?

Use \l or \l+ in psql to show all databases in the current PostgreSQL server. Use the SELECT statement to query data from the pg_database to get all databases.

What is the psql command to show all the relations in a database?

A single Postgres server process can manage multiple databases at the same time. Each database is stored as a separate set of files in its own directory within the server's data directory. To view all of the defined databases on the server you can use the \list meta-command or its shortcut \l .

What is the use of psql command?

psql is a terminal-based front-end to PostgreSQL. It enables you to type in queries interactively, issue them to PostgreSQL, and see the query results. Alternatively, input can be from a file or from command line arguments.


1 Answers

connect to server:

$ mysql -u root -prootpassword  $ su - postgres $ psql 

list databases:

mysql> show databases;  postgres=# \l 

switch databases:

mysql> use ocdp;  postgres=# \c ocdp 

show tables:

mysql> show tables;  postgres=# \dt 

create database:

mysql> create database foo;  postgres=# create database foo; 

drop database:

mysql> drop database foo;  postgres=# drop database foo; 

quit:

mysql> quit  postgres=# \q 
like image 109
SeanPlusPlus Avatar answered Oct 16 '22 06:10

SeanPlusPlus