Antony
Antony

Reputation: 1764

Can't I get to Postgres with plain psql

I always have to give the command like sudo -u postgres psql in order to login into Postgres console. What do I have to in order to login into postgres like sudo psql or psql

The environment I am working on is Ubuntu Linux 12.04

Thanks in advance.

Upvotes: 0

Views: 291

Answers (3)

Daniel Vérité
Daniel Vérité

Reputation: 61526

It's normal that after the installation, only the postgres user is able to do anything with the database server. The installer can't assume that we'd want to open access to anyone else.

To give yourself access as a casual user, assuming as an example that your login name is joe (your normal, non-priviledged user), you just need to create a corresponding user and database:

Inside psql as the postgres administrator (with sudo -u postgres psql), issue:

 CREATE USER joe;
 CREATE DATABASE joe OWNER joe;

After that, when issuing psql at the shell prompt, it will connect by default to your own database with your username. You no longer have to sudo to postgres until you need to issue other administrator commands.

Upvotes: 1

sstn
sstn

Reputation: 3069

Would

psql -U psql

work for you?

EDIT:

I though you would mind about sudo.

If your problem is rather typing -U <user>, you could also set the environment variable PGUSER. This could also be done in your shell's logon script, so that it will always be set.

The other enviroment variables of interest might be PGDATABASE, PGHOST, PGPORT.

Upvotes: 0

Your psql is in /usr/bin/psql. You shouldn't need to use sudo unless your permissions are wrong, or unless your link is wrong. (In later versions of PostgreSQL, /usr/bin/psql is a symbolic link to the executable. I don't know whether that's true in 8.4. On my home computer, it links to /usr/share/postgresql-common/pg_wrapper.)

The full skeleton syntax for psql is

psql -U username -h hostname -p portnumber database_name

So, for example, when I connect to my scratch database (named "sandbox"), I do it like this.

$ psql -U postgres -h localhost -p 5432 sandbox

You would substitute

  • your database username (which must already exist, and which isn't necessarily the same as your network/computer username),
  • your hostname (but "localhost" is probably right for a local install of PostgreSQL),
  • the port PostgreSQL is listening on (but 5432 is probably right; it's the default), and
  • your database name.

Upvotes: 0

Related Questions