Postgresql Ubuntu strange behaviour (version incompatibility?) - postgresql

I'm trying to run a postgresql Database on Ubuntu 16.04 LTS.
First I installed postgres using sudo apt-get install postgresql which installed version 9.5.1. I then created another user and a new database. I've granted all privileges for the new database to new new user and set the owner to the new user also.
I connected to the new database and filled it by restoring an plain backup (dump) I've created from another database (which has postgresql version 9.2) by using \i /path/to/dump.sql. I got no errors, and when I then typen \dt I got the list with the tables.
The problem is: When I now disconnect (\q) and re-connect (same as I connected before, sudo psql -U "username" dbname) and type \dt again, it says "No relations found". When I try to fill it again, I get a bunch of errors like "Relation relationname already exists".
Another problem/symptom appears when I try to use pgAdmin (installed via sudo apt-get install pgadmin3). When trying to connect using localhost, I can't connect using the newly created user (which which I can connect from the terminal). But I can connect using postgres with the password I set via Terminal. I do not see any DB I manually create from command line from pgAdmin tho.
So yeah, my guess is that at least one problem may be version incompatibility between 9.2 and 9.5. Is there any way to test/fix this? But I also think there should be other problems.

What you describe may happen if the SQL dump contains a SET search_path TO...command that sets it to a different value than what your user has by default.
So not only it will create its tables and other objects in that schema, but it will leave that search_path for the rest of the session, so when you do \dt in that same session, it does see and list the newly created tables.
But when you quit and reenter psql, this search_path is not longer in effect, you're back to the default search_path of your user, which supposedly doesn't reach the schema, so \dt no longer "sees" any table.
You can use show search_path to check this setting in a psql session, and grep "SET search_path" in the SQL file to check what it's being set to.
Per comment, it appears to be the case: the dump creates the tables into a schema that is outside of the user's default search path.
A possible solution for that user to work seamlessly would be to update its search path so that it always reaches this schema first. This can be done with:
ALTER USER username SET search_path TO schema1,"$user",public;
where schema1 is what the SQL dump refers to and where it created the tables.

Related

Postgres add user impossible

I get from customer Postgres 8.4, a very strange edition
I login with Postgres user under trust settings.
But in pgAdmin i got a lot of relations errors
I thought normally tables should goes to DB Catalogs Tables, but here DB Catalogs Tables not exists or i cant see them, all tables i see in db Schemas, and database is working as i can access it through ODBC and Postgres connector, but then i see all 2809 tables i a row without separation. It like multi tenat services, but misconfigured or not suit with pgAdmin.
From pgAdmin i cant create any user and roles, from ssh psql created user cant connect through odbc or Postgres connector, and do not appears in pgAdmin Login/Group Roles tab.
So why i cant access in pgAdmin4 Login/Group Roles tab?
Why cant create user from pgAdmin4?
Why cant access pg_catalogs from pgAdmin4?
Why user created from ssh psql cant access database from ODBC or Postgres connector? (got always table is empty)
If this is multi tenant services why it not work like expected from pgAdmin?
PgAdmin 4 version 5.6
PgAdmin4 says it supports 9.5 and newer. 8.4 is not newer than 9.5.

Fail to connect PostgreSQL DB from my Linux User

I am new to technologies , please do not judge my question too strong :).
I installed in My Ubuntu 18.04 PostgreSQL 10.7. To be able to enter my DB I need to enter the following commands from my terminal. sudo -u postgres psql.
Is there any shortened way where I can connect it from my Ubuntu User account. For example. if I input psql it will open database environment where I can type PostgreSQL commands.
Thank you.
Just execute this command in your terminal :
alias psql='sudo -u postgres psql'
So the next time, you input psql and execute, you will be in database environment.
I see two options:
1) Create alias for this command sudo -u postgres psql .
2) Go to psql and create new superuser and database for it:
CREATE ROLE username SUPERUSER;
ALTER ROLE username WITH LOGIN;
CREATE DATABASE username;
You shouldn't be using the superuser account for your normal database work. That is as if you were using root for everything in Linux.
You need to create a regular user with the privileges to create or modify tables in your database. This can be done by granting the user all privileges on the database (which is not the same as making that user a superuser) or make that user the owner of that database.
As documented in the manual psql tries to connect to a database with the name of the current Linux user and with a database user with the name of the current Linux user. So if you want to keep things simple create a user with your regular Linux user's name and an database that is owned by that user:
create user rob password 'somepassword';
create database rob owner = rob;
Assuming your Linux user is rob, then all you need to do is:
psql
and you are connected to a database where you can create and manage tables.
Depending on how you installed Postgres, you might need to adjust pg_hba.conf to allow rob to log in directly.
Again: please do NOT use the superuser account for your normal work.

Not sure if I have PostgreSQL installed on my Mac properly for creating Flask web app

I installed PosgreSQL 10.5 on a Mac running macOS 10.13.6 using Homebrew by following this tutorial. After installation, I ran =# \du to view all users. I saw the user with my Mac's name that the tutorial says PostgreSQL creates during installation, but the "postgres" default user was not created (or at least did not appear).
My Main Issue is...
I need to login to PostgreSQL 'at the root', and I either do not understand exactly what that means, or something is wrong. In following the very first steps from the PostgreSQL wiki's First Steps documentation, it says to...
First connect/login as root:
# su - postgres
$ psql
psql (9.6.0)
Type "help" for help.
But when I open a terminal window and type in su - postgres it asks for a password. I've typed in every password I could possibly have given that role, and it just says su: Sorry.
I did some research and tried to change the password by doing this:
postgres=# \password postgres.
That prompted me to enter a password twice, and it seemed to work. But, when I go back and run: su - postgres, it just gives me the same su: sorry.
So I tried typing in: psql postgres. This at least changed the $ to postgres=#, which tells me that I am at the PostgreSQL command line, and then the same: su: postgres, but that just changed the (=) sign to a (-), so now it changed to: postgres-#
I thought that maybe this meant that I was "logged into PostgreSQL at the root, but then I ran the following:
postgres=# su - postgres
postgres-# CREATE SCHEMA test
postgres-# CREATE USER tester PASSWORD 'P#ssword1'
postgres-# GRANT ALL ON SCHEMA test TO tester
postgres-# \q
...and none of that worked. It did not create a user or a schema.
History...
I've installed/uninstalled PostgreSQL on this Mac several times
in the past for testing
I've also installed/uninstalled pdAdmin for various reasons
It is highly possible that I did something in all of that shuffle that brought about this issue with the 'postgres' role not being created during this Homebrew instal. So, I did read through the rest of that tutorial and I did type in $ createuser postgres and that did seem to create the 'postgres' role. I tried to use ALTER ROLE to give that role Create DB but I never could get it to work using the command line, so I just opened pgAdmin, saw the role, granted it Create DB, and then back in command line ran =# \du again, and Create DB did appear next to the 'postgres' role I'd created. I added everything in pgAdmin, and now it looks like this.
List of roles
Role name | Attributes | Member of
----------------+------------------------------------------------------------+-----------
MyMacsNameHere | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres | Superuser, Create role, Create DB, Replication | {}
My end goal...
I am trying to follow along with this YouTube tutorial about creating a Flask web application from scratch. In the YouTube tutorial, he uses MySQL, but I want to use PostgreSQL. Needless to say, I am up against a learning curve.

psql: FATAL: database "Morne Nel" does not exist

I need to do a report in JasperReports.... besides the point here.
Ive been given a existing Postgresql DB to use.
I have installed PostgreSQL and all went great. (except the package update at the end. Cant get past the proxy server)
Ive added the C:\Program Files\PostgreSQL\9.0\bin path the the system path as-well.
When I go to cmd and type psql it propts for a password....
I enter password, because thats the only password I added during instalation.
Then is comes up with psql: FATAL: database "Morne Nel" does not exist
step by step hoe do I import this DB into postgres? PLEASE?
When using psql you have to pass database name, otherwise postgres will try to connect to database matching your user name (that is why it tries to connect to "Morne Nel").
You can use createdb console command to create database first. Here is documentation on that command: http://www.postgresql.org/docs/9.3/static/sql-createdatabase.html
But also you can use pg_restore command right away with --create option passed to create and restore database from dump at once (documentation here: http://www.postgresql.org/docs/9.3/static/app-pgrestore.html).
Try to execute those commands (it is possible that you will have to specify your postgres user and password) and let us know what happened. I cannot write more at the moment as I am not sure what dump format do you have so it would be good to provide that information if you won't manage to use pg_restore successfully.

PostgreSQL - create a new DB through pgAdmin UI

I have installed PostgreSQL DB server on my Ubuntu machine. Now I want to create a new Database through some GUI application. I tried pgAdmin but didn't find any option to create a new DB. But I could add existing db servers.
Is there any way to create a DB and then tables through pgAdmin or some other app.
Basically I am looking for an application for PostgreSQL like SQLYog for MySQL.
Open pgAdmin
Connect to database server
Edit => New Object => New database
done
Or use plain SQL when connection to any database: CREATE DATABASE my_database;
HI ... Verify that u have done above steps to install the postgresql properly ... and follow the below instructions to create a database in POSTGRESQL
INSTALL POSTGRESQL
1.Install latest PostgreSQL 9.1 in console:
sudo apt-get install postgresql libpq-dev
2.PostgreSQL has a super user is called postgres. Change user to the PostgreSQL user:
sudo su - postgres
3.Change password of postgres user:
psql -d postgres -U postgres
psql (9.1.3) Type "help" for help
postgres=# alter user postgres with password 'YOUR_NEW_PASSWORD';
ALTER ROLE
postgres=# \q
#logout postgres user
logout
4.Restart the PostgreSQL server:
sudo /etc/init.d/postgresql restart
pgAdmin III: PostgreSQL administration and management tools
If pgAdminIII is not installed, the installation is easy:
sudo apt-get install pgadmin3
ADD A SEVER
Open pgAdminIII and add new localhost server. Go to menu File > Add Server
Set up pgAdmin III server instrumentation:
When connecting to a PostgreSQL database using pgAdmin you may receive an error letting you know that the server instrumentation is not installed.
Install postgresql-contrib package:
sudo apt-get install postgresql-contrib
Install adminpack extension:
sudo -u postgres psql
postgres=# CREATE EXTENSION "adminpack";
postgres=# \q
CREATE DATABASE
1.Double Click your database in Left pane on PGAdmin to select it
2.Now click the a Icon named "SQL" ,probably 6th icon, and type CREATE table query in resultant window
EXAMPLE:
3.Create table query
CREATE TABLE explore(
SUBJECT TEXT NOT NULL,
COMPANY CHAR(50) NOT NULL,
PNAME TEXT NOT NULL,
PHONE INT NOT NULL,
EMAIL CHAR(50) NOT NULL,
REMARKS CHAR(200) NOT NULL
);
Post any queries below.............Have a error free day.
One of problems with DB creating is connected to permissions.
If there are no grant for creating database, the path "Object -> Create -> Database" is off (unclickable) and you can't pick this menu option. Likewise, "Databases -> Right Mouse Button" doesn't have "Create" option. One more sign of this case is the reaction in SQL:
CREATE DATABASE DBNAME;
ERROR: permission denied to create database
********** Error **********
To solve the problem you can reconnect to the Server with the first login ('postgres' for example). And afterwards the menu will work.
Of course you can create a database with pgAdmin.
You have to understand the basic concept of PostgreSQL. First you have to connect to a database cluster, which you can only do by connecting to a database in that cluster. Now, if your database does not exist, yet, there is the default maintenance db postgres you can connect to. Connect to it and issue CREATE DATABASE there. Then change to the new database.
On windows right click on the db connector
My db connector is PostgreSQL->Create->Database, expand the database
Next expand schema
under which tables is found, then right click, and create tables
To create tables Expand database->under that expand schema->under tables , click on create tables
To add columns and data types to tables
under the respective table, right click properties and select column fields and add name of columns, data types as needed properties->columns
So, I'm late to the party, but it seems that regular old PG_Admin, and the EnterpriseDB installation are different packages. PG_Admin by itself just manages and connects to servers, but doesn't seem to create them. This is definitely not clear, but yeah you get those errors because you're trying to connect to a server that doesn't exist. You could also use WSL to do this, but you'd have to have WSL and your server up and running everytime to you play.