Log in as root on psql - psql

What does the below command mean that you "login as root"? I don't understand when do you create a user or when you don't have to. If you're logging in as root, does that mean you're just the official super user or something that can create all other users? What's the difference between this and creating other users/roles? If I can get an analogy to understand that would be great.
su - postgres

It switches your unix user and unix shell to the unix postgres user. That's all.
(Don't do that, use sudo -u postgres instead).
At a guess, you're doing that because you then want to run psql. By default on most installs psql will connect to postgres with the same postgres username as your unix user name. So switching to unix user postgres then runnining psql will get you a postgres session as the postgres user, which usually has superuser rights to the database.

Related

psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "myname" does not exist

I a rails app running on my local environment using postgresql. This morning I spun up a new one and after install the pg gem, etc. I am running into the following error when trying to run
psql
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database "jackcollins" does not exist
What's strange is the db name "jackcollins" is from my other rails app.
I ran
pgrep -l postgres
and the output was
20902 postgres
20919 postgres
20920 postgres
20921 postgres
20922 postgres
20923 postgres
20924 postgres
I'm unsure how to proceed so that these apps can both run their own postgres instance.
I had the same problem as you, after making attempts to reinstall, rm -rf xxx.pid, etc., I ended up executing the following command and was eventually able to connect to the PostgreSQL database.
brew install postgresql
an error message appears
run createdb (because macs don't create username databases after installing PostgreSQL)
execute psql to connect successfully.
Regarding the error, you are trying to connect to jackcollins, please can you test trying to connect using the database flag?:
psql -d your_database_name
If you type
psql --help
in the terminal, you will see that the default database is your username. Unless you have that database in PostgreSQL, you will get an error. Instead, you can run the command
psql -d your_database_name
Also, if you want to log in as a specific user (by default it is a current user):
psql -d your_database_name -U your_username -W
The last -W is for password. Hope, it helps!
In the absence of -d <database_name> psql will use the OS user name as the database name. As in many things it better to be explicit rather then implicit, especially when working on a new instance. In addition use -h <host_name>, -p <port_number> and -U <user_name>. Then you know who you are connecting as, as well as how. It is spelled out here psql and since psql is a libpq program in more detail here Key words.

Can't create user on Bitnami Dokuwiki

I've created a Bitnami Dokuwiki Docker container on my Mac using:
curl -sSL https://raw.githubusercontent.com/bitnami/bitnami-docker-dokuwiki/master/docker-compose.yml > docker-compose.yml
docker-compose up -d
I can connect to it in my browser, and there is a login link, but no way I can find to create a user.
Is there a default user, hopefully an admin user? Or do I need to create a user another way?
The default login is superuser, bitnami1.
It sets up this superuser account when you initially create the container. You can change the username and password it uses by passing environment variables DOKUWIKI_USERNAME and DOKUWIKI_PASSWORD to docker with -e.

Unable to restore complete database from pg_dump

I ran the following command to backup my PostgreSQL database:
pg_dump -U postgres -h localhost -W -F t crewdb > /home/chris1/Documents/crewcut/crewdb/crewdb_bak.tar
This file was later saved to a USB.
After installing PostgreSQL on a new Ubuntu 18.04 system I ran the following command to restore the database from the USB:
psql -U postgres -d crewdb < /media/chh1/1818-305D/crewdb_bak.tar
The structure of the database has been recovered, so tables, views etc. except the actual data in the tables which has not been recovered.
Has anyone got an idea why this is and how to solve this.
I do not know if the command you ran to restore your data is correct; on any case try to use the pq_restore as says from the official documentation "restore a PostgreSQL database from an archive file created by pg_dump" that's the correct way to do it.
In my case I use pg_dumpall -U user > backup.sql then "cat backup.sql | psql -U user database"
I recommend you to check out the flags you're using on your pg_dump

How to specify which database (not "master") in tsql (FreeDTS command line tool)

I am actually trying to connect to an MS SQL Server on Azure, from Python, via the module pymssql which relies on FreeTDS. I just can't make it work. I found the command line tool tsql which is supposedly for testing FreeTDS connections. And also, I can't connect with tsql. Regarding this, I have one very specific question.
How do I specify which "database" in the tsql tool. Fx if I use dbeaver, I must specify the database, "ava-iot". Using man tsql does not tell me how to specify another database.
When I try:
$ tsql -H uepbua32ii.database.windows.net -p 1433 -U Azure_SQL_Reader_Temporary -P XXXXXX
I get:
"The server principal "Azure_SQL_Reader_Temporary" is not able to access the database "master" under the current security context."
This tells me, that it is specifically trying to connect to a database named master. So how do I tell it to try the database ava-iot.
The reason this is happening is because your user Azure_SQL_Reader_Temporary has the default database set to be master. You can change that as well. But to answer your question of how to do it with tsql using the -D parameter.
tsql -H uepbua32ii.database.windows.net -p 1433 -D dbname -U Azure_SQL_Reader_Temporary -P XXXXXX
Good luck!

set user and password for `psql`

What is the correct way to run .sql script using psql?
When I do like this
psql D:\scripts\script.sql
It requires password. I pass my db password pass but it shows error
password authentication failed for user PC_ADMIN
(i.e. my windows user). But my db user has the name postgres.
How to set in command psql D:\scripts\script.sql\ user and password?
Try like this:
psql -U username -d myDataBase -a -f script.sql
you have to send the user flag to the script,
psql D:\scripts\script.sql -U whateveruser
OR
psql D:\scripts\script.sql --username=whateveruser
otherwise it will assume that the user is the current OS account.

Resources