o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Database is Used for Each Project or for Each User

Embed Size (px)

Citation preview

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    1/18

    Creating a DatabaseThe first test to see whether you can access the database server is to try to create a database. Arunning PostgreSQL server can manage many databases. Typically, a separate database is used foreach project or for each user.

    Possibly, your site administrator has already created a database for your use. He should have told

    you what the name of your database is. In that case you can omit this step and skip ahead to the nextsection.

    To create a new database, in this example named mydb, you use the following command:

    $ createdb mydbThis should produce as response:

    CREATE DATABASEIf so, this step was successful and you can skip over the remainder of thissection.

    If you see a message similar to

    createdb: command not foundthen PostgreSQL was not installed properly. Either it was not installedat all or the search path was not set correctly. Try calling the command with an absolute pathinstead:

    $ /usr/local/pgsql/bin/createdb mydbThe path at your site might be different. Contact your siteadministrator or check back in the installation instructions to correct the situation.

    Another response could be this:

    createdb: could not connect to database template1: could not connect to server:No such file or directory

    Is the server running locally and acceptingconnections on Unix domain socket "/tmp/.s.PGSQL.5432"?This means that the server was not

    started, or it was not started where createdb expected it. Again, check the installation instructions orconsult the administrator.

    Another response could be this:

    createdb: could not connect to database template1: FATAL: user "joe" does notexistwhere your own login name is mentioned. This will happen if the administrator has not created

    a PostgreSQL user account for you. (PostgreSQL user accounts are distinct from operating systemuser accounts.) If you are the administrator, see Chapter 17 for help creating accounts. You willneed to become the operating system user under which PostgreSQL was installed (usually postgres)to create the first user account. It could also be that you were assigned a PostgreSQL user name thatis different from your operating system user name; in that case you need to use the -U switch or setthe PGUSER environment variable to specify your PostgreSQL user name.

    If you have a user account but it does not have the privileges required to create a database, you willsee the following:

    createdb: database creation failed: ERROR: permission denied to create databaseNot every user has

    authorization to create new databases. If PostgreSQL refuses to create databases for you then thesite administrator needs to grant you permission to create databases. Consult your site administratorif this occurs. If you installed PostgreSQL yourself then you should log in for the purposes of this

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    2/18

    tutorial under the user account that you started the server as. [1]

    You can also create databases with other names. PostgreSQL allows you to create any number ofdatabases at a given site. Database names must have an alphabetic first character and are limited to63 characters in length. A convenient choice is to create a database with the same name as yourcurrent user name. Many tools assume that database name as the default, so it can save you some

    typing. To create that database, simply type

    $ createdbIf you do not want to use your database anymore you can remove it. For example, if you are theowner (creator) of the database mydb, you can destroy it using the following command:

    $ dropdb mydb(For this command, the database name does not default to the user account name.You always need to specify it.) This action physically removes all files associated with the databaseand cannot be undone, so this should only be done with a great deal of forethought.

    More about createdb and dropdb may be found in createdb and dropdb respectively.

    Notes[1] As an explanation for why this works: PostgreSQL user names are separate from operatingsystem user accounts. If you connect to a database, you can choose what PostgreSQL user name toconnect as; if you don't, it will default to the same name as your current operating system account.As it happens, there will always be a PostgreSQL user account that has the same name as theoperating system user that started the server, and it also happens that that user always has

    permission to create databases. Instead of logging in as that user you can also specify the -U optioneverywhere to select a PostgreSQL user name to connect as.

    --------------------------------------------------------------------------------Prev Home NextArchitectural Fundamentals Up Accessing a Database

    --------------------------------------------------------------------------------

    User Comments

    Piyush Nigam 04 Jul 2005 16:09:22

    I installed the database using the rpm packages and ended up with option:createdb: could not connect to database template1: could not connect to server:

    No such file or directoryIs the server running locally and acceptingconnections on Unix domain socket "/tmp/.s.PGSQL.5432"?

    I understand that this is because the database is installed but not started. A link to a page which saidhow to start it would have been good.This is what worked for me:

    1) create the following directory as root in /usr/local/pgsql/data2) Change the owner of this directory to a user called postgres (the rpm installation created thisuser)

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    3/18

    3) Login as postgres and intialise the database with:initdb -D /usr/local/pgsql/data4) Start the database with:

    pg_ctl -D /usr/local/pgsql/data startAfter these steps the createdb command works fineJulien Bidault 27 Jul 2005 21:54:54

    Here is a tip that will save you time :mind by default you are not "postgres" but XXX, so the database doesn't know you!!!

    if installed Postgres and sticked with default option, postgres runs under system user "postgres". Bydefault, the first db user created will also be named "postgres".If your current user account is let's say "rms", you will get an error. All this is explained above, butthe solution is not that well explained. (read chap 17 for overall info).--> The solution is in short you need to create a new user whose name matches your os accountname, in our exemple, rms, and you must do so as "postgres", since as the moment it's the only dbsuper user. The command line is then

    createuser -U postgres -W [rms] -P(replace rms by you userName)

    You will be ask :Password for new User : [type what you want]Confirm new user password : [type the same as above]User can create DB ? [choose y/n as you need]User can create Users ? [choose y/n as you need]Password : [type here the password for postgres, which is the one you chose at install]

    there you are, now you can follow the tutorial, you current user account is now known fromPostgresZeger Hendrikse 27 Aug 2005 23:12:25

    I had difficulties getting started on Fedora Core 4 after successfully installing the RPMs. Whatworked for me was the following:

    - set password for user postgres as root- logged in as user postgres with "su -"- entered the command described above to create a new user,

    where I used the newly created password.

    Karan Malhi 25 Sep 2005 1:58:11

    If you are the root, then just do the following:xyz@localhost: suroot@localhost: su postgres

    postgres@localhost: createuser xyzpostgres@localhost:exitxyz@localhost:psql -W template109 Mar 2007 17:09:16

    Did find Julien Bidault

    27 Jul 2005 21:54:54 comment helpful with following clarifications that might be useful to others:

    1. Postgres does not "know you" directly because it does not read keystrokes keyed at the command

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    4/18

    sytem shell (DOS command window in Windows XP or equivalent). Rather Postress communicatesback and forth with an application (An ".exe" for Windows). Command consoles as such talk to andhear from the operating system via executables (applications). So, via an application, the operatingsystem passes to Postgres the name of the operating-system user (and so, in a way, Postgres does'know who you are'), and the application via the O.S. can return an error message of Postgres to thecommand console that include the OS user's name. This, of course, applies to the installation's

    application creatdb.exe in Windows XP, for example.

    2. When a Postgres role (user) is created with the command:createuser -U postgres -W [rms] -Pand (replace rms by you userName)takes place, where userName is say one's Windows log-in name,make sure not to type the [] square brackets. If one includes them, Postgres will take the bracketsalso as part of the role identifier. But, identifiers with brackets, although taken, as said, by Postgres,may relate to difficulty in dropping them. Of course, if Postgres took the identifier with brackets,the identifier rms will not be known to Postgres.

    New comments cannot be added to old documentation versions.

    Privacy Policy | Project hosted by our server sponsors. | Designed by tinysofa

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    5/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    6/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    7/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    8/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    9/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    10/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    11/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    12/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    13/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    14/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    15/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    16/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    17/18

  • 8/9/2019 o Try to Create a Database. a Running PostgreSQL Server Can Manage Many Databases. Typically, A Separate Dat

    18/18