Couldn't create a new database in Postgres [duplicate] - database

After installing postgresql, I tried it out, typing createdb mydb, like it's written in the documentation. Then the following error occured:
createdb: could not connect to database postgres: FATAL: role "xxx" does not exist
I studied the documentation, where is said:
You will need to become the operating system user under which PostgreSQL was installed (usually postgres) to create the first user account
I tried this by accessing psql (in my case with sudo -u postgres psql, using Ubuntu 12.10).
But then what should I do?

if the db is owned by user postgres you can do the following
createdb -U postgres dbname
since by default postgresql will trust connections from localhost.

su - postgres
and after you have been logged in:
createdb mydb

Related

What are the default SQL Server credentials created via docker?

I'm trying to set up a SQL Server on my raspberry with Ubuntu server 20.04. I've followed both a tutorial and the Microsoft documentation and it runs pretty well.
I've finally run this command :
sudo docker run -e 'MSSQL_PID=developer' -e 'ACCEPT_EULA=Y' -e 'MSSQL_SA_PASSWORD=XXXX' -p 1433:1433 --name azuresqledge -h azuresqledge -d mcr.microsoft.com/azure-sql-edge:latest
I assumed the login would be "developer" and the password "XXX" but it turns out it's not working (Login failed, err n.18456). I tried to access the SQL Server from my computer with SQL Server Management Studio, but it refuses the connection telling me that the credentials are wrong. What can I do?
I also tried with the login "azuresqledge" in doubt, but without result.
Thanks for your help !
The environment variable MSSQL_SA_PASSWORD specifies the password for the sa account. The MSSQL_PID environment value is unrelated an account; it specifies the SQL Server product edition (Developer edition here).
You can provision other accounts after starting the container by connecting with the sa login and executing statements to create logins, database users, etc.. Alternatively, use the SSMS Object Explorer graphical interface for same.
CREATE LOGIN developer WITH PASSWORD = 'password1';
To expand on Dan's answer above which helped me discover the following...
If you need to use a specific login with a password that doesn't meet the requirements, you can turn off the policy like this:
create login admin with password = 'admin', check_policy = off
and if you want it to have sa permissions...
exec master..sp_addsrvrolemember #loginame = 'admin', #rolename = 'sysadmin'

How can I change database inside Shell on Google Cloud Platform using PostgreSQL (psql)?

On GCP, when you connect to Google Cloud Shell, and then connect to PostgreSQL database instance using "gcloud sql connect..." GCP connects you to postgres database.
How can I change to another database already created?
Is there any psql (or specific GCP Shell) command to change database connection?
I have not tried GCP, so I'm not sure if this works there. But in some of my scripts I've the need to do this.
In those scripts, I use
\c DBNAME
More info: How to switch databases in psql?
As #scottsargent answer: \c DBNAME is the solution.
You need on GCP Shell to add a semicolon ; at the end, press ENTER and provide the user password to change database connection when psql ask for it:
postgres=> \c elboticario;
Password for user postgres:
psql (9.6.7, server 9.6.6)
SSL connection (protocol: TLSv1.2, cipher: **************-SHA256, bits: 128, compression: off)
You are now connected to database "elboticario" as user "postgres".
elboticario=>

How to connect to postgresql google cloud sql instance? Need to import postgresql dump using pg_restore command

I have a google cloud sql postgresql instance. When I try to import postgresql dump I get following error.
I am able to connect to instance with the command given below.
gcloud sql connect instance-name --user postgres
It takes me to the psql command line client where I can not use database restore command like pg_restore
Does anyone have an idea on how can I actually connect to Google Cloud SQL instance so that I can perform operation such as pg_restore?
if you need pg_restore can simply use:
gcloud sql connect instance-name --user postgres < dbbackup.sql
where dbbackup.sql is a file obtained from:
pg_dump olddb --format=plain --no-owner -v >> dbbackup.sql

I forgot my sql server 2008 r2 username an password

I installed an instance of sql server on my machine a while back.
I generally login via SSMS. I currently forgot the username and password for the instance.
Is there a way I can recover it. I searched online including a pervious stack overflow post but didnt
find it helpful. Is there anything I can do to recover my username and password ?
Did you try to Troubleshooting: Connecting to SQL Server When System Administrators Are Locked Out
Start the instance of SQL Server in single-user mode by using either
the -m or -f options. Any member of the computer's local
Administrators group can then connect to the instance of SQL Server as
a member of the sysadmin fixed server role.
And then try to do the following steps as explained by Remus Rusanu in this answer.
shutdown MSSQL$EXPRESS service (or whatever the name of your SQL Express service is)
start add the -m and -f startup parameters (or you can start sqlservr.exe -c -sEXPRESS -m -f from console)
connect to DAC: sqlcmd -E -A -S .\EXPRESS or from SSMS use admin:.\EXPRESS
run create login [machinename\username] from windows to create your Windows login in SQL
run sp_addsrvrolemember 'machinename\username', 'sysadmin'; to make urself sysadmin member
restart service w/o the -m -f

How to login and authenticate to Postgresql after a fresh install?

Did a new install of postgres 8.4 on mint ubuntu. How do I create a user for postgres and login using psql?
When I type psql, it just tells me
psql: FATAL: Ident authentication failed for user "my-ubuntu-username"
There are two methods you can use. Both require creating a user and a database.
By default psql connects to the database with the same name as the user. So there is a convention to make that the "user's database". And there is no reason to break that convention if your user only needs one database. We'll be using mydatabase as the example database name.
Using createuser and createdb, we can be explicit about the database name,
$ sudo -u postgres createuser -s $USER
$ createdb mydatabase
$ psql -d mydatabase
You should probably be omitting that entirely and letting all the commands default to the user's name instead.
$ sudo -u postgres createuser -s $USER
$ createdb
$ psql
Using the SQL administration commands, and connecting with a password over TCP
$ sudo -u postgres psql postgres
And, then in the psql shell
CREATE ROLE myuser LOGIN PASSWORD 'mypass';
CREATE DATABASE mydatabase WITH OWNER = myuser;
Then you can login,
$ psql -h localhost -d mydatabase -U myuser -p <port>
If you don't know the port, you can always get it by running the following, as the postgres user,
SHOW port;
Or,
$ grep "port =" /etc/postgresql/*/main/postgresql.conf
Sidenote: the postgres user
I suggest NOT modifying the postgres user.
It's normally locked from the OS. No one is supposed to "log in" to the operating system as postgres. You're supposed to have root to get to authenticate as postgres.
It's normally not password protected and delegates to the host operating system. This is a good thing. This normally means in order to log in as postgres which is the PostgreSQL equivalent of SQL Server's SA, you have to have write-access to the underlying data files. And, that means that you could normally wreck havoc anyway.
By keeping this disabled, you remove the risk of a brute force attack through a named super-user. Concealing and obscuring the name of the superuser has advantages.
by default you would need to use the postgres user:
sudo -u postgres psql postgres
The error your are getting is because your-ubuntu-username is not a valid Postgres user.
You need to tell psql what database username to use
psql -U postgres
You may also need to specify the database to connect to
psql -U postgres -d <dbname>
The main difference between logging in with a postgres user or any other user created by us, is that when using the postgres user it is NOT necessary to specify the host with -h and instead for another user if.
Login with postgres user
$ psql -U postgres
Creation and login with another user
# CREATE ROLE usertest LOGIN PASSWORD 'pwtest';
# CREATE DATABASE dbtest WITH OWNER = usertest;
# SHOW port;
# \q
$ psql -h localhost -d dbtest -U usertest -p 5432
GL
Source
you can also connect to database as "normal" user (not postgres):
postgres=# \connect opensim Opensim_Tester localhost;
Password for user Opensim_Tester:
You are now connected to database "opensim" as user "Opensim_Tester" on host "localhost" at port "5432"
If your database client connects with TCP/IP and you have ident auth configured in your pg_hba.conf check that you have an identd installed and running. This is mandatory even if you have only local clients connecting to "localhost".
Also beware that nowadays the identd may have to be IPv6 enabled for Postgresql to welcome clients which connect to localhost.
With this command below, everyone can log in PostgreSQL just after installation. *The default database postgres is used:
psql -U postgres
You need to put a password after running the command above:
Password for user postgres:

Resources