Shinchan
Shinchan

Reputation: 361

Working with psql, pgadmin and postgreSQL

I am new to PostgreSQL. I have installed PostgreSQL 9.6 and I am using psql as well as PGADMIN4. This is a practice exercise.

I have installed PostgreSQL and by default it installed PGADMIN4 and psql shell. Also it created a user named postgres and a database with name as postgres.

I have created a new user named Joe by right clicking on 'login/group roles' in PGAdmin and then I entered required details for Joe. I want Joe to use postgres db. Hence I right clicked on postgres db under Databases(1) option in PGAdmin and changed the owner from postgres to Joe.

I then opened psql.

enter image description here

It is evident from the image that my username is still postgres. However I have changed it to Joe in pgadmin.

I want it to be Joe.

I am under the impression that if I make some changes in pgadmin then they would reflect in psql.

Correct me if I am wrong.

1) I would like to change my username from postgres to Joe in psql.

2) This Joe should use the db postgres. Joe should be able to create schemas, tables etc in postgres db using psql shell.

Upvotes: 2

Views: 4468

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 247235

You are right with one thing: all changes you make to the database cluster with pgAdmin 4 will be visible with psql and vice versa.

You created a user joe with pgAdmin 4, and the user will be visible with psql: just type \du and the users postgres and joe will be shown.

But you are wrong in assuming that any user you create with pgAdmin 4 will automatically be the default login user used by psql.

Two little thought experiments:

  • What if you create three users with pgAdmin – which of these should be the new default user for psql?
  • psql must specify the database user in the connect packet. How should it know that you created a new user in PostgreSQL before it has even connected to the database?

But it is easy to achieve what you want.

To change the default user that psql uses to connect to the database, set the environment variable PGUSER to joe. In the same fashion, you can change the default database that psql connects to by setting the PGDATABASE environment variable.

Upvotes: 3

Related Questions