How do I connect to database with postgresql version 12 using psql? - postgresql

I want to connect to my newly created database called "test" in psql, I have seen a command like this:
Connection
\c[onnect] {[DBNAME|- USER|- HOST|- PORT|-] | conninfo}
connect to new database (currently "postgres")
What does that mean?
I tried to write:
\c {test|- postgres|- localhost|- 5432|- | conninfo}
but got error:
invalid integer value "5432|-" for connection option "port"
What should I do?

Did you try
\c postgresql://user#localhost/test
Or
\c "host=localhost port=5432 dbname=test connect_timeout=10"
Source

the \c is used when you are already connected to a database and wants to connect to another database.
e.g if you are connected with postgres db and wants to connect with test database then you can do is
\c test
and if using psql then
./psql -U postgres -d test -p 5432

And I just find that this way will work, for all other people like me:
\l
so that you will see a list of dbs that you created, and if you want to see a certain table then
\c db_name
Then you will successfully connect it if the db exists.

My PostgreSQL version is -
psql (15.2 (Ubuntu 15.2-1.pgdg22.04+1))
Connect (\c) is used at psql command prompt and the command syntax is -
\connect (or \c) [ dbname [ username ] [ host ] [ port ] ]
this command takes the value from previous connection for all four input values if we don't pass any value. we can understand this as below -
postgres=# \c (if we don't pass anything on this prompt then it will take all values (db name, user name, host and port) from previous connection.)
postgres=# \c test (here we are passing one value and this value be taken as database name, for other values user name, host name and port will be used from previous connection)
postgres=# \c test testuser (here host and port will be inherit from previous connection while first value will be taken as database name while second value will be taken as user name)
postgres=# \c test testuser localhost (here port will be used from previous connection while other three values db name, user name and host will be taken from given three values)
postgres=# \c test testuser localhost 5432 (in this command all four values are passed hence it will pick from here only)
post execute above command the output is -
postgres=# \c testdb testadmin localhost 5432
Password for user testadmin:
You are now connected to database "testdb" as user "testadmin".

Related

Why is Postgres recognizing user as a database name? [duplicate]

Please check below command which I had run:
C:\Windows\system32>psql -U root
Password for user root:
psql: FATAL: database "root" does not exist
If you don't explicitly specify which database you want to connect to, psql will use the database with the same name as the database user.
Since you specified database user root, psql tries to connect to a database of that name, but the database doesn't exist.
Try specifying an existing database:
psql -U root -d mydb
In case you don't know which database to use, or if you never created a database, you can always use the postgres database.

Username seen as database in psql command?

This question is similar to create database using psql in shell script takes username as db name, but since that doesn't have an accepted answer I'm asking it again here. I've created a user with username myuser and password mypassword:
> psql
psql (11.5)
Type "help" for help.
kurt=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
kurt | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
myuser | | {}
kurt=#
However, if I do psql --username=myuser, I get an error that database "myuser" does not exist:
> psql --username=myuser
psql: FATAL: database "myuser" does not exist
I'm a bit confused by this error message, because according to psql --help, this is a user name, not a database name:
Connection options:
-h, --host=HOSTNAME database server host or socket directory (default: "local socket")
-p, --port=PORT database server port (default: "5432")
-U, --username=USERNAME database user name (default: "kurt")
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
Any idea what I'm doing wrong? Here is the version of psql I'm using:
> psql --version
psql (PostgreSQL) 11.5
Quote from the manual
The default user name is your operating-system user name, as is the default database name
If you specify a username, that username is then also assumed as the default for the database name.
After creating your new user, first you have to login using postgres user and create new database similar to your new created user. ex: if my new user is "david", I must create also database called "david" using posgtres user.After this you can now login using your new user created.

How can I create a Postgres 11 user/role with a different name than the OS-user?

I'm a newbie to PostgreSQL and I'm trying to create a user/role on Postgres 11 (running on CentOS7) which is named (e.g.) "json_docs_admin" (same for password) that I can use under the OS user (e.g.) "app_user" to connect locally to the DB.
I have tried connecting to Postgres using "postgres" user and the command "psql -d template1" and ran the following commands:
template1=# create role json_docs_admin with login password 'json_docs_admin';
CREATE ROLE
template1=# create database json_docs owner=json_docs_admin;
CREATE DATABASE
template1=# GRANT ALL PRIVILEGES ON DATABASE json_docs TO json_docs_admin;
GRANT
I have changed the /var/lib/pgsql/11/data/pg_hba.conf so that local connections can be established on the server, and I've restarted the postgresql service.
Then, back under "app_user" user, when I try to connect to the database, I get the following errors:
$psql -d json_docs -U json_docs_admin
Password for user json_docs_admin:
psql: FATAL: database "json_docs" does not exist
Could anybody let me know what am I doing wrong?
Thanks.

How can I connect to a database as another user?

Im currently creating an API for a school project and everything is working good. My setup is: Node v10, Postgres, Koa and so on...
I currently have this:
CREATE ROLE sa WITH LOGIN PASSWORD 'some-password.';
CREATE DATABASE master WITH OWNER sa;
\c master;
When the init script runs in the docker machine the output I get is this one:
CREATE ROLE
CREATE DATABASE
You are now connected to database "master" as user "postgres".
So I did change the file to something like this:
CREATE ROLE sa WITH LOGIN PASSWORD 'some-password.';
CREATE DATABASE master WITH OWNER sa;
CONNECT TO master AS main USER sa;
And I get a syntax error:
STATEMENT: CONNECT TO master AS sa USER sa;
psql:/docker-entrypoint-initdb.d/init.sql:4: ERROR: syntax error at or near "CONNECT"
I can't find anywhere in docs (or haven't look very good) how to connect from a .sql file to a database with an specific user.
How would I connect to 'master' with its owner, which is 'sa' from a .sql file?
You can do this via this command in psql:
\c db_name usr_name
Here my assumption is you have a SQL file called "a.sql" and contains the lines of code in the file.
CREATE ROLE sa WITH LOGIN PASSWORD 'some-password.';
CREATE DATABASE master WITH OWNER sa;
\c master;
Now you are running this script using "psql" command line interface (CLI), so you get the message as below...
CREATE ROLE
CREATE DATABASE
You are now connected to database "master" as user "postgres".
Now, the thing to be noted that you have connected to database "master" because of "\c master" with user "postgres" because of you passed the credential of "postgres" user. Is I am right here then, If yes, then you have to pass the user and password for different user i.e. "sa" as below.
psql -h localhost -p 5432 -U sa -f a.sql master
Now, it will prompt for the password of "sa" user, finally, your script will run and you will be connected to "sa" user.
You can do this via this command in terminal
psql -d database -U username

Can't access superuser in postgresql

I can't seem to access my superuser account for my posgresQL database.
Using the command:
psql -U postgres
I sucessfully login into the user 'postgres'. However, this is not the default superuser. Doing:
=# \du
I get:
List of roles
Role name | Attributes | Member of
-----------+-------------------------------------+-----------
pgsql | Superuser, Create DB | {}
postgres | Create role, Create DB, Replication | {}
So 'pgsql' appears to be my default superuser.
When trying:
psql -U pgsql
I get the following error:
psql: FATAL: database "pgsql" does not exist
I changed the pg_hba.conf file to the following:
# Database administrative login by Unix domain socket
local all all trust
and also tried:
# Database administrative login by Unix domain socket
local all pgsql trust
but I still get the same error regardless, that database 'pgsql' does not exist.
Any help would be greatly appreciated, I need to be able to access the database's superuser.
just define database:
psql -U pgsql -d postgres
if you get error that postgres database does not exist, connect as postgres and list databases with \l