Receiving 'ERROR: syntax error at or near "sudo"' when trying to create a user in postgres db - postgresql

I'm trying to learn how to configure and setup a Postgres database, I am using WSL/Ubuntu. I am following along with this guide. I'm at the point where I am to create a user but I keep receiving a syntax error and I'm struggling to figure out how I can continue.
postgres=# sudo -u postgres createuser rncongi;
ERROR: syntax error at or near "sudo"
LINE 1: sudo -u postgres createuser rncongi;
Thank you for your help!

sudo is a Linux command, not a SQL command that you can run from within psql.
Additionally createuser is also a command line tool (that needs to run outside of `psql), not a SQL command
But as you are already logged into psql as the superuser, use the SQL command create user to create new user
postgres=# create user rncongi;

Related

Export everything from PostgreSQL

I want to export everything(users, roles, object definitions, data) from my PostgreSQL and restore it into another server. How can I do that?
I try to use
pg_dumpall -U postgres -g > out.sql
but file out.sql is empty. Can someone explain me why this happens?
When I try:
pg_dumpall -U postgres > out.sql
PostgreSQL just didn't want to accept password for "postgres".
I prefer to do this job from (windows) command line(it is not problem and with linux command, I can translate them). And if there is nice pgAdmin way to do it, I will happy to learn it, too.
P.S: My password for "postgres" is correct (I try to login with "psql -U postgres" and there is no problem)
Update: I try with user different than "postgres"(in this case rsmn):
pg_dumpall -U rsmn > out.sql
I have following error:
pg_dumpall: query failed: ERROR: permission dneied for relation
pg_authid pg_dumpall: query was: SELECT oid, rolname, rolsuper,
rolinherit, rolcreaterole, rolcreatedb, rolcanlogin, rolconnlimit,
rolpassword, rolvalidunil, rolreplication, rolbypassrls,
pg_catalog.shobj_description(oid, 'pg_authid') as rolcomment, rolname
= current_user as is_current_user FROM pg_authid WHERE rolname !~ '^pg_' ORDER BY 2
User privileges(This user is only one beside "postgres"):
The attempt with user rsmn fails because it is not a superuser and cannot export the user passwords.
It remains to figure out why pg_dumpall -U postgres fails for you.
For that, the first thing would be to check the PostgreSQL server log. I am not sure where that is on your system, you should check the settings in postgresql.conf (logging_collector, log_destination, log_directory, log_filename). Probably it is in the Windows Event Log.
The log message will tell you what is wrong.
It could well be a problem with the pg_hba.conf file – if so, modern PostgreSQL versions will tell you which line was used.
For debugging purposes, you can try to change the relevant line to trust authentication so that no password is requested (but don't leave it that way, or everybody can connect as user postgres without password!).
If you are trying to do it under the *nix and your server is installed localy, you may not to use ident mode instead of password:
sudo su postgres
pg_dumpall > out.sql

Creating a database dump doesn't seem to be working

I have PostgresQL 9.6.2 installed, and I'm trying to make a backup of one of my databases (my_database) using pg_dump command. It doesn't seem to be working as I see no output file.
I'm using a Mac and from my terminal (and in my project directory) I use:
psql postgres postgres
pg_dump my_database > my_database.bak
being postgres my database default user and password.
I've already tried using sudo psql postgres postgres with the same results.
I imagine that I'm experiencing some kind of lack of permissions but cannot understand it.
So, how to have the right permissions to do this?
Postgres comes with a bunch of stuff, including command-line tools. That includes the standard client, psql, and also the command pg_dump. That needs to be invoked from a shell command line.
If you type it into a psql command line, you'll get a syntax error once you terminate the line (with a ';') -- it's not valid sql, nor a valid command to psql.
Hope that helps!

How to check table value and export the database using PostgreSQL and Ubuntu

I need one help. I need to export one database from my postgreSQL which is running in my ubuntu server.
first i typed the below command
sudo -u postgres psql postgres
it gave me the following result.
I checked all database using below command
\list
It gave me the following result.
I needed to connect the 100salons_production database and i connected using the following command and checked the tables.
\connect 100salons_production
\dt
it gave the following result.
Now i tried to check the table values using this command select * from areas but it does not show any result.
Here I need to check the tables results.and also i need commands to export this database and import it in somewhere else. Please help me to resolve this issue.
If you only want to export the database, there is not need to connect to it using the psql tool. Instead, you have to use the pg_dump tool. You can do that like this:
sudo -u postgres pg_dump DATABASE_NAME > dump.sql
Then you should transfer dump.sql to the other computer and import it there.

PostgreSQL 9.4, 64-bit windows: Unable to create user from commandline

I'm trying to create a user from command line using PostgreSQL version 9.4 on a 64 bit machine with windows.
I'm using the below command to create a new user:
createuser -d temba
On executing the above command, it prompts me for a password.
I enter the password (qwerty) which i used while installing PostgreSQL. on doing so, i get the below error:
psql: FATAL: password authentication failed for user "my-windows-user-name"
Next, i tried giving my login password for windows, i get the same error as above.
Can anyone guide me through the procedure for creating a new user from command line (only, I'm not allowed to use PgAdmin to create user).
I have checked previous posts with similar errors. But the problem there is, either they are not using windows or they are using a really old version of PostgreSql.
Any information on how to proceed about with this shall be really helpful.
Thanks in advance.
All Postgres command line tools try to connect to the database using the current operating system user if no database user is specified.
So you need to specify the user name of the Postgres superuser that is used to connect to the Postgres server:
createuser -U postgres -d temba
This becomes more evident if you use psql instead. You specify the Postgres user account and the target database when you start it:
psql -U postgres -d temba
Then at the prompt you can run create user ....
temba=# create user foobar ... ;
Which is what the command line tool createuser is doing in the background.

Specifying the default database for psql to connect to?

While using psql, I want to change the initial database connection.
I had a database named "test" as the initial connection.
When running psql from the command line my prompt would be test=#
After deleting the "test" database, and running psql in command line, I get the following error:
psql: FATAL: database "test" does not exist
I understand what this means, but how do I go about setting the "postgres" database as the default?
Instead of typing psql postgres each time.
You can configure the default behavior of psql -- and in fact any program using the libpq client library -- through environment variables.
In your ~/.bashrc or similar:
export PGDATABASE=postgres
The PostgreSQL documentation contains a complete list.