Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

on the Linux system; you don’t have to worry about a whole separate set of user accounts.


Another major difference between MySQL and PostgreSQL is that the administrator account in
PostgreSQL is called postgres, not root. When you installed the PostgreSQL package on your
Raspberry Pi, the installation process created a postgres user account on the system so the
PostgreSQL administrative user account can exist.


To interact with the PostgreSQL server, you need to run the psql program as the postres user
account. It looks like this:


Click here to view code image


pi@raspberrypi ~ $ sudo –u postgres psql
psql (9.1.9)
Type "help" for help.

postgres=#

The default psql prompt indicates the name of the database you are connected to. The pound sign
(#) in the prompt indicates that you’re logged in with the administrative user account. To exit the
psql command prompt, you just enter the \q meta-command.


Now you’re ready to start entering some commands to interact with the PostgreSQL server.


Creating a Database


Creating a database in PostgreSQL is the same as in MySQL: All you need to do is submit a CREATE
DATABASE statement. Just remember to be logged in as the postgres administrative account to create
the new database, as shown here:


Click here to view code image


pi@raspberrypi ~ $ sudo -u postgres psql
psql (9.1.9)
Type "help" for help.
postgres=# CREATE DATABASE pytest;
CREATE DATABASE
postgres=#

After you create the database, you can use the \l meta-command to see if your new database appears
in the database listing and then the \c meta-command to connect to it. Here’s an example:


Click here to view code image


postgres=# \l
List of databases
Name | Owner | Encoding
-----------+----------+----------
postgres | postgres | UTF8
template0 | postgres | UTF8
template1 | postgres | UTF8
pytest | postgres | UTF8
(4 rows)

postgres=# \c pytest
You are now connected to database "test" as user "postgres".
pytest=#

When you connect to the pytest database, the psql prompt changes to indicate the new database

Free download pdf